Click here to Skip to main content
13,738,816 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

416.1K views
76 bookmarked
Posted 9 Dec 2010
Licenced CPOL

Browser back button issue after logout

, 26 Jan 2013
Rate this:
Please Sign up or sign in to vote.
How to stop user from accessing the previous pages once he/she logs out and presses browser back button.
This is an old version of the currently published tip/trick.
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?
 
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. Apart from that, caching has to be handled.
One needs to clear the cache 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:
 
// Code disables caching by browser. Hence the back browser button
// grayed out and could not causes the Page_Load event to fire 
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
 
You can add something similar in form aspx if you want to place it there:
 
<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">
OR
one can clear browser history through JavaScript:
//clears browser history and redirects url
<SCRIPT LANGUAGE="javascript"> 
{  
     var Backlen=history.length;   
     history.go(-Backlen);   
     window.location.href=page url 
}
</SCRIPT>
OR
Page.ClientScript.RegisterStartupScript(this.GetType(),"cle","windows.history.clear",true);
OR
one can set this in logout event:
protected void LogOut()   
{       
     Session.Abandon();       
     string nextpage = "Logoutt.aspx";       
     Response.Write("<script language=\"javascript\">");             
     Response.Write("{");       
     Response.Write(" var Backlen=history.length;");       
     Response.Write(" history.go(-Backlen);");       
     Response.Write(" window.location.href='" + nextpage + "'; ");
     Response.Write("}");       
     Response.Write("</script>");   
}
OR
some other combination of the above options.
 
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)

Share

About the Author

Sandeep Mewara
Software Developer (Senior)
India India
Current location: Bangalore, India.

You may also be interested in...

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
 
QuestionDon't touch browser history! - vote 1 Pin
Sinisa Hajnal5-Mar-15 0:15
professionalSinisa Hajnal5-Mar-15 0:15 
QuestionWhere to add the ClearHistory function as suggested in JS based solution? Pin
VICK1-Nov-13 0:41
member VICK1-Nov-13 0:41 
GeneralMy vote of 5 Pin
saguptamca16-Jun-13 23:03
professionalsaguptamca16-Jun-13 23:03 
GeneralMy vote of 5 Pin
grijesh__mnit25-Mar-13 3:43
membergrijesh__mnit25-Mar-13 3:43 
GeneralMy vote of 3 Pin
grijesh__mnit25-Mar-13 3:14
membergrijesh__mnit25-Mar-13 3:14 
GeneralRe: My vote of 3 Pin
Sandeep Mewara25-Mar-13 3:23
mvpSandeep Mewara25-Mar-13 3:23 
GeneralRe: My vote of 3 Pin
grijesh__mnit25-Mar-13 3:46
membergrijesh__mnit25-Mar-13 3:46 
GeneralMy vote of 5 Pin
Renju Vinod28-Feb-13 0:56
memberRenju Vinod28-Feb-13 0:56 
QuestionFirefox Pin
Claes Andskär22-Feb-13 22:26
memberClaes Andskär22-Feb-13 22:26 
QuestionThanks Pin
db7uk21-Feb-13 9:20
memberdb7uk21-Feb-13 9:20 
GeneralMy vote of 1 Pin
Sukh Veer Singh29-Jan-13 22:13
memberSukh Veer Singh29-Jan-13 22:13 
GeneralRe: My vote of 1 Pin
Sandeep Mewara29-Jan-13 23:05
mvpSandeep Mewara29-Jan-13 23:05 
GeneralRe: My vote of 1 Pin
Sukh Veer Singh30-Jan-13 1:59
memberSukh Veer Singh30-Jan-13 1:59 
GeneralMy vote of 4 Pin
Arun Selva Kumar B27-Jan-13 20:16
memberArun Selva Kumar B27-Jan-13 20:16 
GeneralA syntax error Pin
Thomas D [ProgramFOX]5-Jan-13 6:04
memberThomas D [ProgramFOX]5-Jan-13 6:04 
GeneralRe: A syntax error Pin
Sandeep Mewara26-Jan-13 20:08
mvpSandeep Mewara26-Jan-13 20:08 
GeneralRe: Thanks....i have been clear now...... Pin
Pritesh Aryan29-Apr-11 23:25
memberPritesh Aryan29-Apr-11 23:25 
GeneralReason for my vote of 5 good article to understand caching s... Pin
Nikhil_S8-Feb-12 18:52
memberNikhil_S8-Feb-12 18:52 
GeneralReason for my vote of 5 very helping article Pin
ManojDhobale9-Jan-12 17:51
memberManojDhobale9-Jan-12 17:51 
GeneralReason for my vote of 5 Nice tip Pin
nagendrathecoder28-Dec-11 19:49
membernagendrathecoder28-Dec-11 19:49 
GeneralReason for my vote of 5 good one Pin
Prince Antony G13-Dec-11 1:14
memberPrince Antony G13-Dec-11 1:14 
GeneralReason for my vote of 5 Nice.. My 5 Pin
Tejas Vaishnav10-Nov-11 19:15
memberTejas Vaishnav10-Nov-11 19:15 
GeneralReason for my vote of 5 dfdgdfg Pin
member6024-Oct-11 18:08
membermember6024-Oct-11 18:08 
GeneralIf your deleting all of the browser session history, then it... Pin
zenwalker19857-Oct-11 4:39
memberzenwalker19857-Oct-11 4:39 
GeneralReason for my vote of 5 Good tip Pin
Sandesh M Patil29-Jun-11 1:33
memberSandesh M Patil29-Jun-11 1:33 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 27 Jan 2013
Article Copyright 2010 by Sandeep Mewara
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid