【问题标题】:Cookie persistence not working in OWIN authenticationCookie 持久性在 OWIN 身份验证中不起作用
【发布时间】:2020-03-19 10:15:14
【问题描述】:

我正在开发一个使用 OWIN 身份验证系统(Microsoft.Owin v4.1.0 通过 NuGet)的 MVC5 网站。我根据数据库验证用户的登录凭据,然后使用 OWIN IAuthenticationManager 登录。以下是相关代码:

using Microsoft.Owin.Security;

public class AuthManager : IAuthManager
{
    private readonly IAuthenticationManager AuthenticationManager;

    public AuthManager()
    {
        this.AuthenticationManager = HttpContext.Current.GetOwinContext().Authentication;
    }

    public void SignIn(AppUserState appUserState, bool rememberMe)
    {
        AuthenticationProperties authenticationProperties = new AuthenticationProperties()
        {
            AllowRefresh = true,
            IsPersistent = rememberMe,
            ExpiresUtc = DateTime.UtcNow.AddDays(14),
            IssuedUtc = DateTime.UtcNow
        };

        List<Claim> claims = new List<Claim>();
        claims.Add(new Claim(ClaimTypes.NameIdentifier, appUserState.EmailAddress));
        claims.Add(new Claim(ClaimTypes.Name, appUserState.Name));
        claims.Add(new Claim("userState", appUserState.ToString()));

        ClaimsIdentity identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

        this.AuthenticationManager.SignIn(authenticationProperties, identity);
    }
}

登录过程有效,但持久性无效。尽管 IsPersistent 被设置为 true,但在大约 30-60 分钟后会话总是结束。当我进入调试器并检查authenticationProperties 对象时,我可以看到IsPersistent 确实是真的。

我在网上读到,这是决定您的应用程序是否应该让您的用户保持登录状态的标志,即使用户没有积极使用该网站。但是用户的会话总是结束。

我认识到的一件事是AuthenticationProperties 类内部有一个字典对象:

当我打开字典时,我可以看到有一个标记为.persistent 的键,它有一个空白值。我尝试将此值设置为True(以复制.refresh 键的行为)-但这根本没有效果。

我认为上述问题与问题无关,但我认为我应该分享我已经进行的调查。

如何防止用户自动注销?

【问题讨论】:

    标签: c# asp.net-mvc authentication asp.net-mvc-5 owin


    【解决方案1】:

    当您关闭浏览器会话时,持久性 cookie 不会被清除,它们仍然会过期。将CookieAuthenticationOptions 中的ExpireTimeSpan 设置为更长的时间。

    【讨论】:

    • 谢谢,这不是问题的原因,但您确实为我指明了正确的方向。我忘记了Startup.Auth.cs 中列出的默认身份验证步骤。
    【解决方案2】:

    事实证明这与 cookie 过期时间无关。我尝试将 ExpireTimeSpan 设置为 14 天(作为测试),但没有任何区别 - 我仍然自动注销。

    但是我正在查看相同的方法 (ConfigureAuth) 并看到我有以下代码:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        ExpireTimeSpan = new TimeSpan(14, 0, 0, 0, 0),
        LoginPath = new PathString("/Account/Login"),
        Provider = new CookieAuthenticationProvider
        {
            OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                validateInterval: TimeSpan.FromMinutes(30),
                regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
        }
    });
    

    作为测试,我将 validateInterval: TimeSpan.FromMinutes(30), 设置为 3000 分钟。 30 分钟后我不再被注销。所以我的会话结束的原因是因为显然对 SecurityStampValidator.OnValidateIdentity 的调用返回的是当前用户未经授权!

    【讨论】:

      猜你喜欢
      • 2015-09-17
      • 1970-01-01
      • 2017-02-01
      • 2011-06-11
      • 2023-04-05
      • 2018-02-18
      • 2023-03-31
      • 2012-08-01
      • 1970-01-01
      相关资源
      最近更新 更多