【问题标题】:ASP Identity CookieAuthentication not workingASP 身份 CookieAuthentication 不起作用
【发布时间】:2015-11-04 04:24:59
【问题描述】:

所以我有一个 MVC5 Web 应用程序,并且我正在使用内置的 ASP.Identity 框架进行身份验证。我想让我的用户将其登录状态保持 30 天。

这是我在Startup.Auth.cs 中的代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                CookieName = "Test",
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromDays(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                },
                ExpireTimeSpan = TimeSpan.FromDays(30)
            });     

我在登录后检查了 cookie,确实有效期为 30 天,但几分钟/几小时后,当我再次尝试访问该站点时,登录状态消失了。知道为什么吗?

【问题讨论】:

  • 您的用户在数据库的SecurityStamp 字段中是否有任何值?

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


【解决方案1】:

OnValidateIdentity 中似乎有一个错误。每当它尝试重新生成 cookie 时,它​​会将 IsPersistent 设置为 false,即使原始 cookie 是持久的。

如果我没记错的话,您可能会在关闭并重新打开浏览器后遇到此问题。此问题可以通过手动添加 IsPersistent 声明来解决,请尝试查看此link

【讨论】:

    猜你喜欢
    • 2015-04-22
    • 2020-12-25
    • 1970-01-01
    • 2014-11-06
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    相关资源
    最近更新 更多