【问题标题】:How to Invalidate AspNetCore.Identity.Application Cookie after user log out用户注销后如何使 AspNetCore.Identity.Application Cookie 无效
【发布时间】:2022-01-06 13:30:03
【问题描述】:

用户注销后,我无法使 ASP.NET Core Identity 中的 .AspNetCore.Identity.Application cookie 失效。

一旦用户点击退出,下面的代码就会执行。

   public async Task<IActionResult> Logout(LogoutInputModel model)
    {
        // build a model so the logged out page knows what to display
        LoggedOutViewModel loggedOutViewModel = await BuildLoggedOutViewModelAsync(model.LogoutId);

        _logger.LogInformation($"loggedOutViewModel : {JsonConvert.SerializeObject(loggedOutViewModel)}");

        if (User?.Identity.IsAuthenticated == true)
        {
            // delete local authentication cookie
            await _norskTakstSignInManager.SignOutAsync();

            //clear cookies
            var appCookies = Request.Cookies.Keys;
            foreach (var cookie in appCookies)
            {
                Response.Cookies.Delete(cookie);
            }

            // raise the logout event
            await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
        }

        // check if we need to trigger sign-out at an upstream identity provider
        if (loggedOutViewModel.TriggerExternalSignout)
        {
            // build a return URL so the upstream provider will redirect back
            // to us after the user has logged out. this allows us to then
            // complete our single sign-out processing.
            string url = Url.Action("Logout", new { logoutId = loggedOutViewModel.LogoutId });

            // this triggers a redirect to the external provider for sign-out
            return SignOut(new AuthenticationProperties { RedirectUri = url }, loggedOutViewModel.ExternalAuthenticationScheme);
        }

        return View("LoggedOut", loggedOutViewModel);
    }

这成功清除了浏览器中的所有cookie,但是,如果我在退出之前获取名为“.AspNetCore.Identity.Application”的cookie的值,然后将其重新添加到浏览器中,那么我可以无需输入用户凭据即可登录应用程序。

我测试了几个以不同方式设置 cookie 过期时间的流程,但它们似乎都不能正常工作。

我想知道如何使 cookie 无效而不只是清除以解决此问题。然后用户应该无法手动输入 cookie 并登录到系统。非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: c# asp.net-core asp.net-identity


    【解决方案1】:

    这是设计使然...您可以做的一件事是尝试在注销后使用UserManager.UpdateSecurityStampAsync 更新用户的安全标记。

    这样 cookie 的安全标记将与数据库中的安全标记不匹配,并且 cookie 将不再有效(但是,没有其他 cookie 发给该用户,即使他们没有“退出”.. . 因此,如果用户打开了多个会话,所有这些 cookie 都将停止有效,而不仅仅是您退出的那个)。

    Identity 不会跟踪特定的用户会话(它只是针对用户验证 cookie,如果匹配,则匹配)。如果您希望能够有选择地删除会话,则必须自己跟踪它们

    【讨论】:

    • 您好,谢谢。我更新了用户安全标记,但用户仍然可以使用以前的 cookie 登录。我可以在这里查看其他选项吗
    • @Priyankara 不,我不这么认为......请注意,cookie 验证可能会被缓存。我不确定它在 Core 中是如何工作的,但在旧版 ASP.Net 中,有一些 OWIN 服务可以用来使这个缓存失效。同样,如果您需要宏大的会话安全性,最好的办法是自己实现它并使会话 ID 无效(您可以制作一个使其无效的中间件:会话 ID 位于 cookie 中,您可以读取它),但是,存储管理会话由您决定
    • 是的,因为我无法找到确切的解决方案,最终决定使用本手册 implementation。希望这能解决这个问题
    【解决方案2】:

    对我来说,最好的安全做法是将每次登录和注销都保存在一条记录中,并使用唯一的随机 ID 作为 GUID,然后将此“ID 会话”保存到声明中,并在每次用户访问时检查这一点,如果 ID 在声明对该会话是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2021-04-11
      相关资源
      最近更新 更多