ASP.Net: Expiring a page when navigating back

10,185

The only way you can consistently do this is if you are using https. If not you have no way to enforce the browser to not use a cached page. There are the hacks you mentioned about but they are not full proof. If it is really important, use https because each request will force a reload.

Share:
10,185
K2so
Author by

K2so

A pineapple juice vendor who has an interest in answering and asking technical questions on Stackoverflow

Updated on June 05, 2022

Comments

  • K2so
    K2so almost 2 years

    Basically all pages on this site I am building cannot be accessed when the user clicks on "Back" (or with key control) in the browser, and the page should expire if one is trying to navigate back in history.

    I put into Global.asax::Application_BeginRequest

        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1))
        Response.Cache.SetValidUntilExpires(False)
        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches)
        Response.Cache.SetNoStore()
    

    This would clear out the cache and disallow going back to any pages when the user is logged out, but doesn't do the job while the user is logged in.

    I saw posts where people suggested using a javascript approach, by calling

        History.Forward(1)
    

    on the page. But I wouldn't like to do this, as it will require javascript enabled to work (which user can disable).

    Appreciate any suggestions.

  • JoeBilly
    JoeBilly about 14 years
    Agreed. You can control the page expiration on your web server (by headers/metas or code) but you cannot do anything about client and network persistance behaviors (bowsers cache, proxies cache...).
  • K2so
    K2so about 14 years
    Thanks. Appreciate your advice. Just to add, the site is already using https already. And yes, like Kelsey mentioned, it is forcing a reload, but I am still able to click Resend (on firefox) and the posted data will be resent as a result and the page is reloaded with the data. On a page without any posts, it is not even confirming and just displaying page as it was. I did more lookups, and seems like this is a common question, but without a true solution. Hacks like the javascript I mentioned originally seems to be the most suggested workaround so far. I guess there isn't any other known way?