Click here to Skip to main content
15,867,985 members
Articles / Web Development / ASP.NET
Tip/Trick

Browser Back Button Issue after Logout

Rate me:
Please Sign up or sign in to vote.
4.89/5 (72 votes)
20 Feb 2013CPOL2 min read 541K   75   53
How to stop user from accessing the previous pages once he/she logs out and presses browser back button
In this tip, you will find a resolution to handle browser's back button once a user has logged out.

Introduction

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:

C#
// 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:

HTML
<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:

JavaScript
//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:

C#
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:

C#
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.

History

  • 9th December, 2010: Initial version

License

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


Written By
Architect Intuit India
India India


A software professional for more than 17+ years building solutions for Web and Desktop applications.

Currently working at Intuit India.

Website: Learn By Insight
Github: Sandeep Mewara
LinkedIn: Sandeep Mewara

Strongly believe in learning and sharing knowledge.



Comments and Discussions

 
GeneralReason for my vote of 5 good one Pin
Prince Antony G13-Dec-11 1:14
Prince Antony G13-Dec-11 1:14 
GeneralReason for my vote of 5 Nice.. My 5 Pin
Tejas Vaishnav10-Nov-11 19:15
professionalTejas Vaishnav10-Nov-11 19:15 
GeneralReason for my vote of 5 dfdgdfg Pin
member6024-Oct-11 18:08
member6024-Oct-11 18:08 
GeneralIf your deleting all of the browser session history, then it... Pin
zenwalker19857-Oct-11 4:39
zenwalker19857-Oct-11 4:39 
GeneralReason for my vote of 5 Good tip Pin
Sandesh M Patil29-Jun-11 1:33
Sandesh M Patil29-Jun-11 1:33 
General@Sandeep, Thanks for replying......one more question i have ... Pin
Pritesh Aryan29-Apr-11 22:06
Pritesh Aryan29-Apr-11 22:06 
GeneralRe: It would depend on the way you do it. If you use History bac... Pin
Sandeep Mewara29-Apr-11 22:24
mveSandeep Mewara29-Apr-11 22:24 
General@Sandeep.. Though i have not much experience in WEB...i woul... Pin
Pritesh Aryan29-Apr-11 18:56
Pritesh Aryan29-Apr-11 18:56 
@Sandeep..
Though i have not much experience in WEB...i would like to ask one question.....
What if user want to retain their Browser History and Cache?
GeneralRe: Thats a the design constraint. I clearly mentioned it is not... Pin
Sandeep Mewara29-Apr-11 19:40
mveSandeep Mewara29-Apr-11 19:40 
GeneralReason for my vote of 5 Good tip for beginners 'How handle ... Pin
TweakBird17-Mar-11 8:35
TweakBird17-Mar-11 8:35 
GeneralVery good, 5. --SA Pin
Sergey Alexandrovich Kryukov20-Feb-11 20:32
mvaSergey Alexandrovich Kryukov20-Feb-11 20:32 
GeneralReason for my vote of 5 Nice and valuable tip Pin
Sreejith Gopinathan16-Feb-11 23:10
Sreejith Gopinathan16-Feb-11 23:10 
GeneralReason for my vote of 5 Nice tip, Sandeep! --SA Pin
Sergey Alexandrovich Kryukov4-Feb-11 9:47
mvaSergey Alexandrovich Kryukov4-Feb-11 9:47 
GeneralReason for my vote of 5 Very Nice Tip, Thanks for Sharing. I... Pin
Gandalf_TheWhite30-Jan-11 19:11
professionalGandalf_TheWhite30-Jan-11 19:11 
GeneralReason for my vote of 5 Veru useful information, Sandeep, th... Pin
Sergey Alexandrovich Kryukov27-Jan-11 20:47
mvaSergey Alexandrovich Kryukov27-Jan-11 20:47 
GeneralReason for my vote of 5 Thanks for sharing this tip Pin
linuxjr27-Jan-11 16:53
professionallinuxjr27-Jan-11 16:53 
GeneralReason for my vote of 1 None of the ways are fool proof. Pin
dan!sh 27-Jan-11 10:04
professional dan!sh 27-Jan-11 10:04 
GeneralEncouraging developers to mess with browser history is a bad... Pin
Chris Maunder23-Jan-11 19:56
cofounderChris Maunder23-Jan-11 19:56 
GeneralRe: Thanks for your suggestion Chris. I have updated the Tip/Tri... Pin
Sandeep Mewara27-Jan-11 6:53
mveSandeep Mewara27-Jan-11 6:53 
GeneralRe: Thanks for your suggestion Chris. I have updated the Tip/Tri... Pin
Suraj8520-May-12 22:25
Suraj8520-May-12 22:25 
GeneralBut Dear Sandeep, it doesn't work with Google Chrome or Fire... Pin
Abhijat Chauhan20-Jan-11 1:07
Abhijat Chauhan20-Jan-11 1:07 
GeneralReason for my 5: Good tip. Very valuable infomation. Thanks ... Pin
Manfred Rudolf Bihy1-Jan-11 6:29
professionalManfred Rudolf Bihy1-Jan-11 6:29 
GeneralReason for my vote of 5 Good one! This is a very valuable Ti... Pin
Manfred Rudolf Bihy1-Jan-11 6:28
professionalManfred Rudolf Bihy1-Jan-11 6:28 
GeneralReason for my vote of 5 Nice one Sandeep!! This is very comm... Pin
Brij20-Dec-10 20:39
mentorBrij20-Dec-10 20:39 
GeneralReason for my vote of 5 Nice One Pin
Vigneshb620-Dec-10 19:54
Vigneshb620-Dec-10 19:54 

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.