使asp.net web应用脱机

使asp.net web应用脱机,asp.net,Asp.net,基本上是一个我们分发给客户的web应用程序,其中一个客户将试用它,所以我需要能够在某一点关闭它。我不想在web.config中输入结束日期,以防他们发现他们可以更改它,我想在global.asax中输入硬编码日期,但我不确定如何“关闭”应用程序。我想在“身份验证请求”部分检查日期,然后简单地重定向到一个页面,上面显示您的试用已完成(或类似内容),但有没有更好的方法?您可以在global.asax上这样做: protected void Application_BeginRequest(Objec

基本上是一个我们分发给客户的web应用程序,其中一个客户将试用它,所以我需要能够在某一点关闭它。我不想在web.config中输入结束日期,以防他们发现他们可以更改它,我想在global.asax中输入硬编码日期,但我不确定如何“关闭”应用程序。我想在“身份验证请求”部分检查日期,然后简单地重定向到一个页面,上面显示您的试用已完成(或类似内容),但有没有更好的方法?

您可以在
global.asax
上这样做:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
   if(DateTime.UtcNow > cTheTimeLimitDate)
   {
        HttpContext.Current.Response.TrySkipIisCustomErrors = true;
        HttpContext.Current.Response.Write("...message to show...");
        HttpContext.Current.Response.StatusCode = 403;
        HttpContext.Current.Response.End();
        return ;    
   }    
}
这比将其放在web.config上更安全,但没有什么是足够安全的。更好的办法是将他们重定向到一个页面,或者不向他们显示消息,或者不管你怎么想

对于make redirect to a page,您还需要检查调用if是否针对某个页面,代码如下:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
   string cTheFile = HttpContext.Current.Request.Path;
   string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
   if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
   {
     // and here is the time limit.
     if(DateTime.UtcNow > cTheTimeLimitDate)
     {
        // make here the redirect
        HttpContext.Current.Response.End();
        return ;    
    }    
  }
}
为了使其更加困难,您可以创建一个自定义的基本页面,使所有页面都来自它(而不是来自
System.Web.UI.page
),并在那里设置页面呈现的限制,或者在每个页面呈现的顶部显示一条消息,表示时间已结束

public abstract class BasePage : System.Web.UI.Page
{
    protected override void Render(System.Web.UI.HtmlTextWriter writer)        
    {
        if(DateTime.UtcNow > cTheTimeLimitDate)
        {
            System.IO.StringWriter stringWriter = new System.IO.StringWriter();

            HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

            // render page inside the buffer
            base.Render(htmlWriter);

            string html = stringWriter.ToString();

            writer.Write("<h1>This evaluation is expired</h1><br><br>" + html);         
        }
        else
        {
            base.Render(writer);
        }
    }
}
公共抽象类基页:System.Web.UI.Page
{
受保护的覆盖无效渲染(System.Web.UI.HtmlTextWriter编写器)
{
如果(DateTime.UtcNow>cTheTimeLimitDate)
{
System.IO.StringWriter StringWriter=新的System.IO.StringWriter();
HtmlTextWriter htmlWriter=新的HtmlTextWriter(stringWriter);
//在缓冲区内呈现页面
base.Render(htmlWriter);
字符串html=stringWriter.ToString();
writer.Write(“此评估已过期

”+html); } 其他的 { base.Render(writer); } } }
只需添加app_offline.htm,您甚至可以为用户创建一条好消息。此外,很容易将网站恢复在线,只需删除或重命名app_offline.htm即可


你的意思是说,你将不托管它&你的客户端将自己托管代码?该应用程序安装在客户端服务器上。如果你试图使
应用程序离线.html
,那么用户可以删除在那里制作文件的权限,这样他们就可以避免。他想给用户一个试用型应用程序。这对他来说不是锻炼,因为任何ASP.NET开发人员都应该知道,删除app_offline.htm将使网站恢复在线。