如何在注销时清除会话

65

当用户点击注销时,我会将用户重定向到登录页面,但是我认为它并没有清除任何应用程序或会话数据,因为所有数据都在用户重新登录后保留。

当前登录页面有一个登录控件,页面的代码只与登录身份验证相关联。

请问有人能向我推荐有关处理ASP.NET网站登录和注销的好教程或文章吗?

10个回答

71

10
我尝试使用 Session.Abandon 方法清除会话,但仍然无法清除会话。 - Jack
1
发生了奇怪的事情,因为Session.Abandon()应该给用户一个新会话。如果您找到更多/更好的数据,请发布它,我相信社区会尽力帮助解决。 - Ryan Cook

35

我使用以下代码来清除会话并清除aspnet_sessionID

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

3
对此点赞:非常好的回答,这是唯一干净的方法。如果不将ASP.NET_SessionId设置为空字符串,则仍将使用旧的会话ID(可以使用开发人员工具栏F12、网络、详细信息进行验证)。我之前尝试过只使用.Clear.Abandon,但确实需要这第三步。 - Matt

21
我更倾向于使用Session.Abandon()Session.Clear()不会触发End事件,而客户端的进一步请求也不会引发Session Start事件。

14

Session.Abandon() 销毁会话并触发 Session_OnEnd 事件。

Session.Clear() 只是从对象中删除所有值(内容)。具有相同键的session仍然存在。

因此,如果使用 Session.Abandon(),则会失去该特定会话,并且用户将获得一个新的会话密钥。例如,当用户注销时可以使用它。

如果要保留用户的会话(例如,您不希望他重新登录)并重置其所有会话数据,请使用 Session.Clear()


2
我认为这是目前为止最好的答案。 - Geeky Guy

3
清除会话的方法在.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()

点击此处查看 API 参考文档

要求 .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();

点击此处查看API参考文档


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>  

1

session.abandon()不会从浏览器中删除sessionID cookie。因此,此后的任何新请求都将使用相同的会话ID。 因此,在session.abandon()之后使用Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""))。


1

针对 .Net Core

[HttpPost]
    public IActionResult Logout()
    {
        try
        {
            CookieOptions option = new CookieOptions();
            if (Request.Cookies[AllSessionKeys.AuthenticationToken] != null)
            {
                option.Expires = DateTime.Now.AddDays(-1);
                Response.Cookies.Append(AllSessionKeys.AuthenticationToken, "", option);
            }

            HttpContext.Session.Clear();
            return RedirectToAction("Login", "Portal");
        }
        catch (Exception)
        {
            throw;
        }
    }

0

Session.Clear();


1
Session.Abandon()会移除会话和事件。.Clear并不能清除所有内容。 - JoshYates1980

-1

进入你的项目中的文件 Global.asax.cs,并添加以下代码。

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

这对我有用..! 参考链接 在MVC 4上注销时清除会话


3
请注意,这段代码仅仅是防止用户的浏览器缓存数据,并不会清除任何会话数据。如果不小心应用此代码可能会对性能产生影响。 - Oskar Berggren
仅用于缓存,不用于会话。 - MorgoZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接