Click here to Skip to main content
Click here to Skip to main content

Browser back button issue after logout

By , 20 Feb 2013
 
Well, I found a lot of people asking for a resolution to handle the browser's back button once user has logged out.
 
Typically, users report 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 that 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.

So, what's the basic reason behind it? It's, browser's Cache!
Now, what can be done to handle the scenario? Surely on logout event one does clear the session. Post which, 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.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
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"/>
Option #3: Clear browser's history through JavaScript using script tag
//clears browser history and redirects url
<SCRIPT LANGUAGE="javascript">
function ClearHistory()
{
     var backlen = history.length;
     history.go(-backlen);
     window.location.href = loggedOutPageUrl
}
</SCRIPT>
Option #4: Clear browser's history through JavaScript injecting through code-behind file via Response
protected void LogOut()
{
     Session.Abandon();
     string loggedOutPageUrl = "Logout.aspx";
     Response.Write("<script language="'javascript'">");
     Response.Write("function ClearHistory()");
     Response.Write("{");
     Response.Write(" var backlen=history.length;");
     Response.Write(" history.go(-backlen);");
     Response.Write(" window.location.href='" + loggedOutPageUrl + "'; ");
     Response.Write("}");
     Response.Write("</script>");
}
Option #5: Clear browser's history through JavaScript injecting through code-behind file via Page.ClientScript
Page.ClientScript.RegisterStartupScript(this.GetType(),"clearHistory","ClearHistory();",true);
UPDATE:
As Chris[^] suggested, I would like to add here itself as a part of the Tip/Trick that I would not suggest to use it and mess with browser's history as this is a bad, bad thing.
One should implement it, only if they really need it and are prepared to accept that it is not a good practice.
 
Lastly, this would not work if one disables JavaScript.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Member
From Bangalore, India.
My blog: http://smewara.wordpress.com/

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membergrijesh__mnit25 Mar '13 - 3:43 
Nice techniques.
GeneralMy vote of 3membergrijesh__mnit25 Mar '13 - 3:14 
Good Article. Got number of solutions. It would be much helpful if OP suggest pros/cons for each method.
GeneralRe: My vote of 3mvpSandeep Mewara25 Mar '13 - 3:23 
grijesh__mnit wrote:
Good Article. Got number of solutions.

Your comment and vote does not match. Do share across if I am missing something.
 
Thanks for your feedback.
Sandeep Mewara
Microsoft ASP.NET MVP 2012 & 2013

[My Blog]: Sandeep Mewara's Tech Journal!
[My Latest Article]: HTML5 Quick Start Web Application

GeneralRe: My vote of 3membergrijesh__mnit25 Mar '13 - 3:46 
No actually I should take time to learn. You have posted good solutions. I am new to the web stuffs.
 
BTW: here is one link related link: Making sure a web page is not cached, across all browsers
 
http://stackoverflow.com/questions/49547/making-sure-a-web-page-is-not-cached-across-all-browsers[^]
GeneralMy vote of 5memberRenju Vinod28 Feb '13 - 0:56 
Nice 5
QuestionFirefox [modified]memberClaes Andskär22 Feb '13 - 22:26 
Thank you for this very helping article. Just what I need this morning.
But I still have problems with Firefox , it just don't work.
 
And in IE you have a drop down right of the history buttons, can I clear this too?
 
best Regards from Sweden
 
I put Option #1 in Code behind and I think Firefox now work.
 
/Claes

modified 23 Feb '13 - 4:55.

QuestionThanksmemberdb7uk21 Feb '13 - 9:20 
This is good for people to see as it is a frequently asked question! good work!
GeneralMy vote of 1memberSukh Veer Singh29 Jan '13 - 22:13 
none of the method is working for me
GeneralRe: My vote of 1mvpSandeep Mewara29 Jan '13 - 23:05 
Thanks for your vote.
 
Sorry but I cannot help you much with the details your have shared across. Nor is it possible for me to assume or see what you have implemented that is 'not working'.
Sandeep Mewara
Microsoft ASP.NET MVP 2012 & 2013
 

[My Latest Article(s)]:
How to extend a WPF Textbox to Custom Picker
Server side Delimiters in ASP.NET

GeneralRe: My vote of 1memberSukh Veer Singh30 Jan '13 - 1:59 
sir what kind of info i have to share with you?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 20 Feb 2013
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid