[英]ASP.NET Identity 2 Remember Me - User Is Being Logged Out
我在我的 MVC5 应用程序中使用 Identity 2.1。 我将 PasswordSignInAsync 的 isPersistent 属性设置为 true 以启用“记住我”:
var result = await SignInManager.PasswordSignInAsync(model.Username,
model.Password,
true,
shouldLockout: false);
但是如果我在夜间保持登录状态,那么当我早上刷新页面时,它会将我注销,我必须再次登录。 在用户手动注销之前,如何防止自动注销?
是否与身份使用的 Cookie 身份验证有关? 我不太了解 Startup.Auth.cs 中设置的 CookieAuthenticationOptions。
new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user)
=> user.GenerateUserIdentityAsync(manager))
}
我认为你应该阅读这篇文章。 有两种不同的时间间隔: ValidateInterval
和ExpireTimeSpan
。 在您的情况下,我认为您应该更改expireTimeSpan
,而不是ValidateInterval
。
在类似问题中有对TimeSpan
参数的解释。 只需使用无限 cookie,如下所示:
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(0),
regenerateIdentity: (manager, user)
=> user.GenerateUserIdentityAsync(manager))
这也是它正常工作所必需的:
称呼
await UserManager.UpdateSecurityStampAsync(userId);
前
AuthenticationManager.SignOut();
从这篇文章中, isPersistent
参数设置身份验证会话是否跨多个请求持久化。
我有这个问题。 这是因为我的自定义 UserStore 没有实现 IUserSecurityStampStore。
public Task<string> GetSecurityStampAsync(IdentityUser user)
{
return Task.FromResult<string>(user.SecurityStamp);
}
如果没有安全标记,SecurityStampValidator 就没有什么可验证的,因此注销用户。
我应该多写点。 这个奇怪的代码:
OnValidateIdentity = SecurityStampValidator
.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
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)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.