【问题标题】:identity 2.2 logging off doesn't destroy session身份 2.2 注销不会破坏会话
【发布时间】:2018-07-31 22:11:33
【问题描述】:

在对我们的系统进行渗透测试后,有人指出,当用户注销时,该用户会话仍然处于活动状态,尽管 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 中的错误,还是我遗漏了什么?

我将 Identity 2.2.1 与 Entity Framework 6 和 MVC 5 一起使用

【问题讨论】:

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


    【解决方案1】:

    Identity 不使用会话,因此如果您使用,则终止会话将是您的责任。

    但是,当 Identity 确实注销时,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 中登录,然后从 Chrome 中退出,IE cookie 也会失效。

    【讨论】:

    • 感谢您的回答,我使用的是默认的 Cookie 身份验证。然而它没有用。我仍然可以使用邮递员查看受限页面。
    • 您能否将validateInterval 设置为TimeSpan.FromMinutes(0),确保数据库中的安全标记实际上已更改,并且您可以从HttpContext.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;() 方法中获取非空对象?
    • 感谢 Trailmax,Timespan 完成了最后一招。
    • 我在 asp.net core @trailmax 上遇到同样的问题...你能帮我解决这个问题吗
    猜你喜欢
    • 2013-02-12
    • 2016-02-03
    • 1970-01-01
    • 2019-05-17
    • 2012-11-25
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多