I have found a lot of people asking for a resolution to handle the browser’s back button once user has logged out. Typically, users reports something like:
I am facing issue in my application’s logout scenario. After the user login into website, he/she uses it(website) and when done, they do a logout, which leads them back to login page. But the problem is now, from this login page, if I click the browser back button then it again takes the user back to the previous visited page as if logged in. How can I stop user from viewing the previous page once logged out?
The browser Back button is an option to go back to previously visited pages. The back button can be considered as a pointer that is linked to the page previously visited by the user. Browser keeps a stack of the web pages visited as a doubly-linked list.
The back button works by stepping through the history of HTTP requests which is maintained by the browser itself. This history is stored in browsers cache that consists of the entire page content with resources like image and scripts. This enables browser to navigate backwards and forwards through the browser history and have each page displayed instantly from cache without the delay of having it retransmitted over the internet from the server.
Just to handle the scenario of getting page content from server, browsers have a Refresh button transmits the request to web server and get back the fresh copy of entire page. Internally, this also replaces the copy of the page in the browser’s cache.
Thus, basic reason behind the problem in discussion is Browser’s Cache!
On Logout, one does clear the session to make sure user related data no more exists. Now, browsers cache needs to be handled such that browser has no history (this will make back/forward button in browser grayed out disabled.) Here are various ways of how one can do it:
Option #1: Set Response Cache settings in code-behind file for a page
// Code disables caching by browser.
Option #2: Set META tag for HTTP cache settings in your ASPX page header
<META Http-Equiv="Cache-Control" Content="no-cache"/>
<META Http-Equiv="Pragma" Content="no-cache"/>
<META Http-Equiv="Expires" Content="0"/>
//clears browser history and redirects url
var backlen = history.length;
window.location.href = loggedOutPageUrl
protected void LogOut()
string loggedOutPageUrl = "Logout.aspx";
Response.Write(" var backlen=history.length;");
Response.Write(" window.location.href='" + loggedOutPageUrl + "'; ");
Any one or combination of above can be used to handle the scenario.