SharpCoder
SharpCoder

Reputation: 19183

How to clear / abandon Session if user clicks on browser Back button in asp.net

Need to clear session on browser back button (redirect to Login or Error Page), and if user paste the same URL again in the browser to access the web page or click on browser forward button, because the session has expired / abandoned so redirect to Login or Error page. The same or any application web page should not be shown from the browser cache at any point of time.

Technology Stack:

Upvotes: 0

Views: 5208

Answers (2)

nima
nima

Reputation: 6733

I don't think disabling cache is a good idea. The only thing important to you is that the user should have to login and start over when he hits navigation buttons. You need to have a signout page that does whatever you need to do to sign out and clear session, Then redirect to that page when the user navigates away.

$(window).bind('beforeunload', function (event) {
   //if(some condition that detects user hasn't clicked a link on your page)
       location.href = "/signout.aspx";
}

Upvotes: 0

semao
semao

Reputation: 1757

Unfortunately most of modern browsers will reload page from cache after user clicks back button. There is no request send to the server.

You can try disabling cache by addind meta information in html head section:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

but keep in mind that its the browser that decides what content to reload from cache and what content request again.

Upvotes: 3

Related Questions