3

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?

3
  • You mean to say, you will not be hosting it & your clients will host the code themselves? Commented Oct 25, 2012 at 8:28
  • the app is installed on the clients server Commented Oct 25, 2012 at 8:34
  • If you try to make the app_offline.html then the user can remove permissions to make files there, so they can avoid that. Commented Oct 25, 2012 at 8:36

2 Answers 2

3

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);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

He wants to give the users a trial kind of application. This will not workout for him as any ASP.NET developer should know removing the app_offline.htm will get back the site online.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.