Disabling browser's back functionality on sign out from Asp.Net






2.80/5 (38 votes)
Aug 5, 2005
3 min read

263707

3567
After sign out from site if browsers back button is clicked it shows the previous page, though user is sign out from the site, to avoid this disabling of cache is done
Introduction
Disabling the Back Button
When i was doing coding for sign in and sign out for my clients application, i found that after signing out from the application i transfered the control to login page e.g. login.aspx. At this point if i click the Back button of Browser it shows the content of previous page user was viewing.
As there was important data displayed on page it is security threat.
It is a threat for web applications displaying important information like credit card numbers or bank account details.
I tried to find the solution on net but could not get satisfactory answer.
On searching i found following problem, and i think it is important to share this issue-
When we visit a page it is stored in a cache i.e. history on a local machine. Caching of Web Pages can happen in three separate entities in a Web environment. To avoid the displaying of page on click of back button we have to remove it from cache, or have to tell the server not to cache this page. So if we do not cache the page then on click of back button request goes to server side and validation can be done whether session exist's or not. This can be achieved by adding following code in the page load of the page for which we do not want to cache the page in history. Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);What happens when Back Button clicked
Caching-
Solution-
Response.Buffer=<SPAN style="COLOR: blue">true;<o:p></o:p>
Response.ExpiresAbsolute=DateTime.Now.AddDays(-1d);
Response.Expires =-1500;
Response.CacheControl = "no-cache";
if(Session["SessionId"] == null)
{
Response.Redirect ("WdetLogin.aspx");
}
}
Code in Detail-
Response.Expires = -1500;
Response.CacheControl = "no-cache";
Things can work with only one line of code
i.e. Response.CacheControl = "no-cache";
But it is good practice to delete the existing page from cache.
This code will tell the server not to cache this page, due to this when user clicks the Back button browser will not find the page in cache and then will go to server side to get the page.
Disabling cache can also be done by adding following line in
<METAHTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
But when I tried this it does not worked for me.
Proxy Server Caching-
Response.CacheControl = "private";
It disables the proxy server caching and page is cached on local machine.
Response.CacheControl = "public";
Proxy server cache is enabled.
Users request pages from a local server instead of direct from the source.
So if the information displayed is critical information extra care should be taken to remove the page from cache on sign out.
Hence for such applications keeping pages non caching is good solution.