ASP.NET身份2记住我 – 用户被注销
我在我的MVC5应用程序中使用Identity 2.1。 我将PasswordSignInAsync的isPersistent属性设置为true以启用“记住我”:
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, true, shouldLockout: false);
但是,如果我一夜之间保持登录状态,那么当我在早上刷新页面时,它会将我退出,我必须再次登录。 在用户手动注销之前,如何防止自动注销?
是否与身份使用的Cookie身份validation有关? 我真的不了解Startup.Auth.cs中设置的CookieAuthenticationOptions。
new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator .OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) }
我想你应该读这篇文章 。 有两个不同的间隔: ValidateInterval
和ExpireTimeSpan
。 在你的情况下,我认为你应该改变expireTimeSpan
,而不是ValidateInterval
。
类似问题中有TimeSpan
参数的解释。 只需使用无限的cookie,如下所示:
OnValidateIdentity = SecurityStampValidator .OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(0), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
这也是它正常工作所必需的:
呼叫
await UserManager.UpdateSecurityStampAsync(userId);
之前
AuthenticationManager.SignOut();
我应该写更多。 这个奇怪的代码:
OnValidateIdentity = SecurityStampValidator .OnValidateIdentity( validateInterval: TimeSpan.FromMinutes(0), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
导致我的应用程序在1天后丢失了cookie。 我真的不知道为什么,但在排除这些代码并在我的web.config中添加一个mashine密钥之后“记住我”未来终于正常工作了。
我目前的代码是:
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), ExpireTimeSpan = TimeSpan.FromDays(5) });
在此post中 , isPersistent
参数设置是否在多个请求中持久保存身份validation会话。