2008-12-05 205 views
53

當用戶單擊註銷時,我將用戶重定向到登錄頁面,但我不認爲它會清除任何應用程序或會話,因爲當用戶重新登錄時所有數據都會持續。如何在註銷時清除會話

當前登錄頁面有一個登錄控件,頁面後面的代碼只與登錄驗證有關。

有人可以指導我一個很好的教程或有關處理登錄和退出ASP.NET網站的文章嗎?

回答

66
+7

我嘗試Session.Abandon,但它仍未清除會話。 – Jack 2008-12-05 23:14:06

+0

發生了一些奇怪的事情,因爲Session.Abandon()應該給用戶一個新的會話。如果你發現更多/更好的數據,也許你會遇到不同的問題:發佈它,我確信社區會盡力幫忙。 – 2008-12-06 02:20:06

0

Session.Clear();

+0

Session.Abandon()刪除會話和事件。 。清除不了所有東西。 – JoshYates1980 2017-01-04 19:14:08

18

我寧願Session.Abandon()

Session.Clear()不會造成完火,並進一步要求從客戶端不會引發Session Start事件。

1
<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> 
10

Session.Abandon()破壞了會議,並觸發Session_OnEnd事件。

Session.Clear()只是刪除Object中的所有值(內容)。 session with the same key仍然是alive

因此,如果您使用Session.Abandon(),則會丟失該特定會話,並且用戶將得到new session key。你可以使用它,例如當用戶logs out

使用Session.Clear(),如果您希望用戶保持在同一會話中(例如,如果您不希望他重新登錄)並重置所有會話特定數據。

+0

我認爲這是迄今爲止最好的答案。 – Renan 2016-12-26 14:26:42

18

我使用後清除會話,並明確aspnet_sessionID

HttpContext.Current.Session.Clear(); 
HttpContext.Current.Session.Abandon(); 
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", "")); 
+2

對此+1:非常好的答案,它是唯一干淨的方法。無需將ASP.NET_SessionId設置爲空字符串,舊會話ID仍將被使用(可使用開發人員工具欄F12,網絡,詳細信息進行驗證)。我以前只用`.Clear`和`.Abandon`嘗試過,但這第三步真的很需要。 – Matt 2015-07-14 10:28:23

2

轉到文件的Global.asax.cs在您的項目並添加以下代碼。

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

它爲我工作..! 參考鏈接 Clear session on Logout MVC 4

0

清除會話的方式與.NET核心有一點不同。沒有Abandon()函數。

ASP.NET核心1.0或更高

//Removes all entries from the current session, if any. The session cookie is not removed. 
HttpContext.Session.Clear() 

See api Reference here

.NET框架4。5或更高

//Removes all keys and values from the session-state collection. 
HttpContext.Current.Session.Clear(); 

//Cancels the current session. 
HttpContext.Current.Session.Abandon(); 

See api Reference here