Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi there,

I am designing a website.Everything works fine except the logout scenario.After the user login into website,he uses it(website) and does a logout to return to Login page,but the problem is that from login page if I click the back button,again it redirects to the last used page.This should not happen.The session still exists.I used
Session.Abandon();
Response.Redirect("Login.aspx");
in Logout linkbutton click event.But the problem still exists.
Is there any other solution for this and Can I use Session Timeout mechanisms.Please let me know..
Thanks and Regards,
S.M.Naresh.
Posted

1 solution

This happens because of cache.
1. clear the sessions
2. clear the cache such that browser has no history (this will make back/forward button in browser grayed out disabled.)

Code for clearing cache can be put up in code behind as follows:
C#
// 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();


OR
You can add somethin 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"></meta></meta></meta>


OR
You can clear browser history through JavaScript....
JavaScript
//clears browser history and redirects url
<SCRIPT LANGUAGE=javascript> {  var Backlen=history.length;   history.go(-Backlen);   window.location.href=page url }</SCRIPT>

OR as you say in you logout event:
C#
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>");   
}
 
Share this answer
 
Comments
S.M.Naresh 18-Nov-10 0:15am    
Hi Sandeep, Your solution is very nice.But the problem here would be that the user may completely loose his browsing history(of other sites atleast),but I want to make the user go to SessionExpire Page if he clicks back button after he do logout.From there we can provide a link to Login again..How can we do this..?
thatraja 18-Nov-10 0:41am    
Excellent answer sandeep

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900