How to clear a session on logout

I redirect the user to the login page when the user logs out, however I do not think that he clears any application or session, because all data is saved when the user logs in.

Currently, there is an input control on the login page, and the code on the page is only connected to the login authentication.

Can someone refer me to a good tutorial or article on handling login and logout from ASP.NET websites?

+51
Dec 05 '08 at 21:12
source share
7 answers
+64
Dec 05 '08 at 21:15
source share

I would prefer Session.Abandon()

Session.Clear() will not start End, and further requests from the client will not trigger a session start event.

+18
Dec 05 '08 at 21:18
source share

I use the following to clear the session and clear the aspnet_sessionID :

 HttpContext.Current.Session.Clear(); HttpContext.Current.Session.Abandon(); HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", "")); 
+17
Nov 14 '14 at 18:13
source share

Session.Abandon() destroys the session and the Session_OnEnd event Session_OnEnd .

Session.Clear() simply removes all values ​​(contents) from the object. session with the same key is still alive .

So, if you use Session.Abandon() , you will lose this particular session and the user will get a new session key . You can use it, for example, when the user logs out .

Use Session.Clear() if you want the user to remain in the same session (if you do not want him to translate the example) and reset all his data related to the session.

+10
Aug 18 2018-12-18T00:
source share

Go to the Global.asax.cs file in your project and add the following code.

  protected void Application_BeginRequest() { Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.Now.AddHours(-1)); Response.Cache.SetNoStore(); } 

It worked for me ..! Link Link Clear Session When Exiting MVC 4

+2
Jan 6 '16 at 6:00
source share
 <script runat="server"> protected void Page_Load(object sender, System.EventArgs e) { Session["FavoriteSoftware"] = "Adobe ColdFusion"; Label1.Text = "Session read...<br />"; Label1.Text += "Favorite Software : " + Session["FavoriteSoftware"]; Label1.Text += "<br />SessionID : " + Session.SessionID; Label1.Text += "<br> Now clear the current session data."; Session.Clear(); Label1.Text += "<br /><br />SessionID : " + Session.SessionID; Label1.Text += "<br />Favorite Software[after clear]: " + Session["FavoriteSoftware"]; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>asp.net session Clear example: how to clear the current session data (remove all the session items)</title> </head> <body> <form id="form1" runat="server"> <div> <h2 style="color:Teal">asp.net session example: Session Clear</h2> <asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="DarkMagenta" > </asp:Label> </div> </form> </body> </html> 
+1
May 25 '11 at 12:33
source share

Session.Clear ();

0
Dec 05 '08 at 21:14
source share



All Articles