身份 2.2 注销不会破坏会话

identity 2.2 logging off doesn't destroy session

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

这是我当前的注销方法:

        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 中的错误还是我遗漏了什么?

我在 Entity Framework 6 和 MVC 5

中使用 Identity 2.2.1

Identity 不使用 session,因此如果您使用,您有责任终止 session。

但是,当身份注销时,cookie 已过期。但是登录后可以使用相同的 cookie 值。要缓解这种情况,您可以更新用户 SecurityStamp

await userManager.UpdateSecurityStampAsync(userId);

并确保您在 Startup.Auth.cs

中激活了 SecurityStampValidator
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中是logged-in,然后从Chrome注销,IE cookie也会失效。