ASP.NET How to Expire Pages / Back Button Issue

You want to expire the pages of your site anytime the users presses the back-button but for some reason the pages are always still there.

I found this works best if you place the expiration logic in the onload method. I have tried onIint but it seems on_load works close to all the time. In the example below a MasterPageBase class has the expiration logic. All master pages then inherit this page.

namespace MySite
{
    public class MasterPageBase : System.Web.UI.MasterPage
    {       
        protected override void OnLoad(EventArgs e)
        {
            //Expire the page to stop the back and forward button usage.
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddHours(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            
            if (!Page.IsPostBack) PageSetup();

            //Load derived
            base.OnLoad(e); 
        } 
        private void PageSetup()
        {           
            if (Context.Session != null)
            {
                if (Session.IsNewSession) if (!URL.ToLower().Contains("default.aspx")) Response.Redirect("default.aspx");
            }
       }     
    }
}
Comments are closed