Basically a web app that we distribute to clients, one of whom will be trialling it so I need to be able to switch it off at a certain point. Don't want to put the end date in the web.config in case they work out they can change it, I was thinking of putting something in the global.asax with a hard coded date, but then I'm not sure how I can 'turn off' the app. I was thinking of checking the date in the Authenticate Request part and simply redirecting to a page that says your trial is finished (or something similar), but is there a better way?
2 Answers
You can do that on global.asax as:
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 ;
}
}
this is safer than place it on web.config, but nothing is safe enough. Its even better there to redirect them to a page, or not show them a message, or what ever you think.
For make redirect to a page you also need to check if the call if for a page, and the code will be as:
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 ;
}
}
}
To makes it even harder, you can make a custom BasePage that all page come from it (and not from System.Web.UI.Page) and you place there the limit on the render of the page - or show a message on top of every page render, that the time is ends.
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);
}
}
}
Comments
Just add the app_offline.htm and you can even create a nice message for your users. Also it's very easy to put the site back online, just remove or rename the app_offline.htm.
http://weblogs.asp.net/dotnetstories/archive/2011/09/24/take-an-asp-net-application-offline.aspx
app_offline.htmlthen the user can remove permissions to make files there, so they can avoid that.