题
当用户单击注销时,我将用户重定向到登录页面,但我认为它不会清除任何应用程序或会话,因为当用户重新登录时所有数据仍然存在。
目前登录页面有一个登录控件,页面后面的代码仅连接登录身份验证。
有人可以指导我阅读有关处理 ASP.NET 网站登录和注销的优秀教程或文章吗?
解决方案
Session.Abandon()
http://msdn.microsoft.com/en-us/library/ ms524310.aspx
下面是HttpSessionState
对象上再详细一点:
http://msdn.microsoft.com/ EN-US /库/ system.web.sessionstate.httpsessionstate_members.aspx
其他提示
我用以下以清除会话和明确aspnet_sessionID
:
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
我宁愿Session.Abandon()
Session.Clear()
不会造成完火,并从客户端进一步请求不会提高会话开始事件。
Session.Abandon()
破坏会话和触发事件Session_OnEnd
Session.Clear()
只是从对象中删除所有的值(内容)。所述session with the same key
仍然alive
。
所以,如果你使用Session.Abandon()
,你失去了特定的会话和用户会得到一个new session key
。你可以使用它例如,当用户logs out
。
使用Session.Clear()
,如果希望留在同一个会话的用户(如果你不想让他重新登录,例如)和重置所有他会话特定的数据。
转到文件的Global.asax.cs 在您的项目并添加以下代码。
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddHours(-1));
Response.Cache.SetNoStore();
}
它为我..! 参考链接 上注销MVC 4 清除会话
<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>
.NET core 的清除会话的方式略有不同。没有 Abandon()
功能。
ASP.NET Core 1.0 或更高版本
//Removes all entries from the current session, if any. The session cookie is not removed.
HttpContext.Session.Clear()
.NET Framework 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.Clear();
session.abandon()不会删除来自浏览器的cookie的会话ID。因此,在此之后的任何新的请求将采取相同的会话ID。 因此,使用Response.Cookies.Add(新的HttpCookie( “ASP.NET_SessionId”, “”));后session.abandon()。