Asp.net 如何在注销时清除会话
当用户单击“注销”时,我将用户重定向到登录页面,但是我认为它不会清除任何应用程序或会话,因为当用户重新登录时,所有数据都会保留下来 目前,登录页面有一个登录控件,页面上的代码仅连接到登录验证 有人能告诉我一篇关于处理ASP.NET网站登录和注销的好教程或文章吗?Session.Clear() 下面是关于Asp.net 如何在注销时清除会话,asp.net,.net,asp.net-session,Asp.net,.net,Asp.net Session,当用户单击“注销”时,我将用户重定向到登录页面,但是我认为它不会清除任何应用程序或会话,因为当用户重新登录时,所有数据都会保留下来 目前,登录页面有一个登录控件,页面上的代码仅连接到登录验证 有人能告诉我一篇关于处理ASP.NET网站登录和注销的好教程或文章吗?Session.Clear() 下面是关于HttpSessionState对象的更多细节: 我更喜欢会话.放弃() Session.Clear()不会导致端到端触发,客户端的进一步请求不会引发会话启动事件。 <script run
HttpSessionState
对象的更多细节:
我更喜欢
会话.放弃()
Session.Clear()
不会导致端到端触发,客户端的进一步请求不会引发会话启动事件。
<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>
受保护的无效页面加载(对象发送方,System.EventArgs e){
会话[“FavoriteSoftware”]=“Adobe ColdFusion”;
Label1.Text=“会话读取…
”;
标签1.Text+=“最喜欢的软件:”+会话[“最喜欢的软件”];
Label1.Text+=”
SessionID:“+Session.SessionID;
Label1.Text+=“
现在清除当前会话数据。”;
Session.Clear();
Label1.Text+=”
SessionID:“+Session.SessionID;
Label1.Text+=“
常用软件[清除后]:”+会话[“常用软件”];
}
asp.net会话清除示例:如何清除当前会话数据(删除所有会话项)
asp.net会话示例:会话清除
会话.放弃()
销毁会话,并触发会话ONED
事件
Session.Clear()。使用相同键的会话仍处于活动状态
因此,如果您使用会话.放弃()
,您将丢失该特定会话,用户将获得一个新会话密钥
。例如,当用户注销时可以使用它
如果您希望用户保持在同一会话中(例如,如果您不希望他重新登录),请使用Session.Clear()
,并重置其所有会话特定数据。我使用以下命令来清除会话并清除aspnet\u sessionID
:
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
转到项目中的文件Global.asax.cs,添加以下代码
protected void Application_BeginRequest()
{
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddHours(-1));
Response.Cache.SetNoStore();
}
这对我很有用。。!
参考链接
对于.NET core来说,清除会话的方式有点不同。没有放弃()
函数
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();
会话.放弃()不会从浏览器中删除会话ID cookie。因此,在此之后的任何新请求都将采用相同的会话ID。
因此,请使用Response.Cookies.Add(新的HttpCookie(“ASP.NET_SessionId”);session.about()之后。我尝试了session.about,但它仍然没有清除会话。发生了一些奇怪的事情,因为session.about()应该给用户一个新会话。如果你发现了更多/更好的数据,也许你会有不同的问题:发布它,我相信社区会尽力帮助你。+1对于这一点:非常好的答案,这是唯一干净的方法。如果不将ASP.NET_SessionId设置为空字符串,则仍将使用旧的会话ID(可通过开发人员工具栏F12、网络、详细信息验证)。我以前只尝试过.Clear
和放弃
,但这第三步确实是必要的。只是为了澄清一下,请注意,此代码不清除任何会话数据-它只会阻止用户的web浏览器缓存数据,如果不小心应用,可能会影响性能。而不是会话,仅针对缓存。我认为这是目前为止最好的答案。Session.discard()删除会话和事件。Clear并不能得到一切。
//Removes all keys and values from the session-state collection.
HttpContext.Current.Session.Clear();
//Cancels the current session.
HttpContext.Current.Session.Abandon();