使asp .net Web应用程序脱机

时间:2012-10-25 08:15:10

标签: asp.net

基本上是我们分发给客户的网络应用程序,其中一个将试用它,所以我需要能够在某个时刻关闭它。不想把结束日期放在web.config中以防万一它们可以改变它,我想把一些东西放在带有硬编码日期的global.asax中,但后来我不确定我是怎么做的可以'关闭'应用程序。我正在考虑在Authenticate Request部分中检查日期,只是重定向到一个页面,说明您的试用已经完成(或类似的东西),但是有更好的方法吗?

2 个答案:

答案 0 :(得分:3)

您可以在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上更安全,但没有什么是安全的。它更好地将它们重定向到一个页面,或者不向它们显示消息,或者你想到的是什么。

要重定向到页面,您还需要检查是否为页面调用,代码如下:

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 ;    
    }    
  }
}

为了让它变得更难,您可以创建一个自定义的BasePage,所有页面都来自它(而不是来自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);
        }
    }
}

答案 1 :(得分:0)

只需添加app_offline.htm,您甚至可以为用户创建一条好消息。此外,将网站重新上线非常容易,只需删除或重命名app_offline.htm即可。

http://weblogs.asp.net/dotnetstories/archive/2011/09/24/take-an-asp-net-application-offline.aspx