身份2.2注销不会破坏会话

时间:2018-02-21 10:52:18

标签: c# session asp.net-mvc-5 asp.net-identity-2

在对我们的系统进行笔测试之后,有人指出,当用户注销时,该用户会话仍处于活动状态,尽管cookie已被删除。我通过复制.AspNet.ApplicationCookie值来确认这一点,并向Postman的其他受限制站点发出请求。在调试时,我可以看到即使在我放弃会话之后,会话仍然存在ID。

这是我当前的LogOff方法:

        public ActionResult LogOff()
    {
        HttpContext.Response.Cache.SetAllowResponseInBrowserHistory(false);
        HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
        HttpContext.Session.Clear();
        HttpContext.Session.Abandon();
        HttpContext.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
        return RedirectToAction("Index", "Home");
    }

我想知道这是否是Identity或MVC中的错误,还是我错过了什么?

我正在使用Identity 2.2.1与Entity Framework 6和MVC 5

1 个答案:

答案 0 :(得分:1)

身份不使用会话,因此如果您使用会话,则您有责任终止会话。

但是,当Identity注销时,cookie已过期。但是,登录后可以使用相同的cookie值。为了缓解这个问题,您可以更新用户的SecurityStamp

await userManager.UpdateSecurityStampAsync(userId);

并确保在SecurityStampValidator

中激活Startup.Auth.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(1),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
    },
    // other stuff
});

但请注意,更新安全标记将使所有浏览器中的所有用户cookie无效。因此,如果用户登录Chrome和IE,然后从Chrome注销,IE Cookie也将失效。

相关问题