当前位置: 首页 > 编程语言 > C#

ASP.NETIdentity2记住我-用户已注销Share

时间:2023-04-11 11:30:22 C#

ASP.NETIdentity2记住我-用户已注销我在我的MVC5应用程序中使用Identity2.1。我将PasswordSignInAsync的isPersistent属性设置为true以启用“记住我”:varresult=awaitSignInManager.PasswordSignInAsync(model.Username,model.Password,true,shouldLockout:false);但是,如果我整夜保持登录状态,那么当我早上刷新页面时,它会将我注销,我必须重新登录。在用户手动注销之前,如何防止自动注销?是不是和Identity使用的cookie认证有关?我不太了解Startup.Auth.cs中设置的CookieAuthenticationOptions。newCookieAuthenticationProvider{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))这也是它工作所必需的:调用awaitUserManager.UpdateSecurityStampAsync(userId);在AuthenticationManager.SignOut()之前;我应该写更多。这个奇怪的代码:OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(validateInterval:TimeSpan.FromMinutes(0),regenerateIdentity:(manager,user)=>user.GenerateUserIdentityAsync(manager))导致我的应用程序在1天后丢失cookie。我真的不知道为什么,但是在排除这些代码并在我的web.config中添加一个mashine键之后,“记住我”的未来终于可以正常工作了。我当前的代码是:app.UseCookieAuthentication(newCookieAuthenticationOptions{AuthenticationType=DefaultAuthenticationTypes.ApplicationCookie,LoginPath=newPathString("/Account/Login"),ExpireTimeSpan=TimeSpan.FromDays(5)});在这篇文章中,isPersistent参数设置是否在多个请求中保持身份验证会话。以上就是C#学习教程:ASP.NETidentity2记住我-用户已注销分享全部内容,如果对你有用还需要了解更多C#学习教程,希望大家多多采纳更多关注~本文来自网络收集,不代表立场,如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢