【问题标题】:Why log out sooner than ExpireTimeSpan in asp.net identity?为什么在 asp.net 身份中比 ExpireTimeSpan 更早注销?
【发布时间】:2017-12-13 03:33:51
【问题描述】:

我正在开发一个使用 asp.net 身份的 asp.net mvc 应用程序。
Startup.Auth.cs 文件中,我将ExpireTimeSpan 设置为20 天,但是当我登录到我的应用程序时,不到20 天我的应用程序就被注销了,我必须再次登录!

Startup.Auth.cs

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/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, User>(
            validateInterval: TimeSpan.FromMinutes(0),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    },
    ExpireTimeSpan = TimeSpan.FromDays(20),
    SlidingExpiration = true
});

Login 行动中:

var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);

更新
当我登录时,会生成.AspNet.ApplicationCookie,并且它的过期日期设置为“20”天后,当我第二天打开网站时,我已经注销但 cookie 存在。

这个问题的原因是什么?
提前致谢。

【问题讨论】:

  • model.RememberMe 是否设置为 true 并且您确定在调用 PasswordSignInAsync 时它是 true
  • 多快?
  • @CodingYoshi 是的,RememberMe 设置为 true,我确定!
  • @sepehr 我将ExpireTimeSpan 设置为 20 天,但我必须每天至少登录一次!
  • @MajidBasirati 您是否将machineKey 添加到webConfig 文件并检查它是否有效?

标签: asp.net-mvc cookies asp.net-identity


【解决方案1】:

以下是提前退出的几个原因:

  • 在同一个域中有多个 Web 应用程序,并且它们都具有 the same cookie name(cookie 名称冲突)。在这种情况下,应用 A 会覆盖应用 B 的 cookie。

  • validateInterval设置为零/TimeSpan.FromMinutes(0)时,所有对UpdateSecurityStamp的调用都会强制用户立即注销并重新登录,包括UserManager.CreateAsyncUserManager.RemovePasswordAsyncUserManager.UpdatePassword、@ 987654328@、UserManager.ChangePhoneNumberAsync/SetPhoneNumberAsyncUserManager.SetTwoFactorEnabledAsyncUserManager.SetEmailAsync。这意味着如果您更新用​​户的属性,UpdateSecurityStamp 将被调用。

  • 如果您更新服务器上的.NET framework,它也会覆盖machine-key。更改此设置会将所有已发布的 cookie 标记为无效。机器密钥是一组用于加密和解密 cookie 的密钥。如果您在负载均衡器后面运行,则需要确保网络场使用一致的machine-key
  • 如果您在 cookie 中存储了过多的 user-claims,它们会变大(大于 ~5K)并且某些浏览器会拒绝它们。所以检查发出的 cookie 的大小。
  • 用户可以将浏览器设置为在关闭 cookie 时删除 cookie(隐私浏览)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2021-06-08
    • 2021-06-19
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    相关资源
    最近更新 更多