当用户单击注销时,我将用户重定向到登录页面,但我不认为它会清除任何应用程序或会话,因为当用户重新登录时所有数据都会持续。如何在注销时清除会话
当前登录页面有一个登录控件,页面后面的代码只与登录验证有关。
有人可以指导我一个很好的教程或有关处理登录和退出ASP.NET网站的文章吗?
当用户单击注销时,我将用户重定向到登录页面,但我不认为它会清除任何应用程序或会话,因为当用户重新登录时所有数据都会持续。如何在注销时清除会话
当前登录页面有一个登录控件,页面后面的代码只与登录验证有关。
有人可以指导我一个很好的教程或有关处理登录和退出ASP.NET网站的文章吗?
Session.Abandon()
http://msdn.microsoft.com/en-us/library/ms524310.aspx
这里是HttpSessionState
对象上更详细一点:
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate_members.aspx
我宁愿Session.Abandon()
Session.Clear()
不会造成完火,并进一步要求从客户端不会引发Session Start事件。
<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>
Session.Abandon()
破坏了会议,并触发Session_OnEnd
事件。
Session.Clear()
只是删除Object中的所有值(内容)。 session with the same key
仍然是alive
。
因此,如果您使用Session.Abandon()
,则会丢失该特定会话,并且用户将得到new session key
。你可以使用它,例如当用户logs out
。
使用Session.Clear()
,如果您希望用户保持在同一会话中(例如,如果您不希望他重新登录)并重置所有会话特定数据。
我认为这是迄今为止最好的答案。 – Renan 2016-12-26 14:26:42
我使用后清除会话,并明确aspnet_sessionID
:
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
对此+1:非常好的答案,它是唯一干净的方法。无需将ASP.NET_SessionId设置为空字符串,旧会话ID仍将被使用(可使用开发人员工具栏F12,网络,详细信息进行验证)。我以前只用`.Clear`和`.Abandon`尝试过,但这第三步真的很需要。 – Matt 2015-07-14 10:28:23
转到文件的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
清除会话的方式与.NET核心有一点不同。没有Abandon()
函数。
ASP.NET核心1.0或更高
//Removes all entries from the current session, if any. The session cookie is not removed.
HttpContext.Session.Clear()
.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();
我尝试Session.Abandon,但它仍未清除会话。 – Jack 2008-12-05 23:14:06
发生了一些奇怪的事情,因为Session.Abandon()应该给用户一个新的会话。如果你发现更多/更好的数据,也许你会遇到不同的问题:发布它,我确信社区会尽力帮忙。 – 2008-12-06 02:20:06