Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii any one can tell me logout code for web applicatio. i am doing logout but when i click on Back button of browser then It shows back page again. How can i remove..Thnxx..
Posted
Updated 30-Apr-19 5:33am

C#
protected void btnlogout_Click(object sender, EventArgs e)
       {
           Session.Abandon();
           Session.Clear();
           Response.Redirect("LoginPage.aspx");
       }
 
Share this answer
 
Comments
D.K.Pareek 11-Jan-14 6:10am    
@vidyasagar back button is working till Nw..after this code..
The other poster is right. You CANNOT stop the back button working. Solutions you find on the web, are easily hacked. All you can do, is write code to log the user out, and mark your pages to not be cached, so that they reload when the user tries to go back ( and thus go to your login page ).

http://www.htmlgoodies.com/beyond/reference/article.php/3472881[^]
 
Share this answer
 
Use this code in your logout code


C#
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
 
Share this answer
 
Adding an CP reference article which deals with the same issue.

Browser Back Button Issue After Logout[^]

Hope this helps...
 
Share this answer
 
Check Browser back button issue after logout[^] and follow the technique.
 
Share this answer
 
in the page load event write following code
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (Session["loginstatus"].ToString() == "")
    {
        Response.Redirect("Login.aspx");
    }
}


and write following code in logout button click event

C#
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
Session.Clear();
        Response.Redirect("Login.aspx");
    }


then at the time of login set
Session["loginStatus"] = "ok";
 
Share this answer
 
v2
This issue happens as once the user logs out, the page can still gets served from the cache. Hence the cache needs to be cleared once the user logs out.please include below lines of code in your logout function to clear cache. 

HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate"); HttpContext.Current.Response.AddHeader("Pragma", "no-cache"); HttpContext.Current.Response.AddHeader("Expires", "0");
 
Share this answer
 
Comments
CHill60 1-May-19 6:08am    
Clearing the cache has already been stated and several different ways of achieving that have already been suggested

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