如何解决ASP.NET 身份更改密码使用户退出
我遇到了 ASP.NET 身份提供程序的问题。我从 Visual Studio 2019 附带的默认模板开始,针对 .NET 4.7.2 WebForms。
在更改密码页面上,身份提供商似乎没有更新 .AspNet.ApplicationCookie
以反映新的 SecurityStamp
。我可以在 fiddler 中看到 ASP.NET 正在发送 302 以重定向回 Manage.aspx 页面,并且它正在发送一个身份验证 cookie,这大概应该反映密码更改导致的新 SecurityStamp
,除了它没有。因此,用户会在密码或 2FA 更改后立即退出应用程序,因为我已将 validateInterval
设置为 TimeSpan.Zero
。如果我将其设置为另一个值,则用户的 cookie 在该值之后将变得无效,而不管 cookie 应该具有的实际年龄。
app.UseCookieAuthentication(New CookieAuthenticationoptions() With {
.AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,.ExpireTimeSpan = TimeSpan.FromDays(14),.SlidingExpiration = True,.Provider = New CookieAuthenticationProvider() With {
.OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(Of ApplicationUserManager,ApplicationUser)(
validateInterval:=TimeSpan.Zero,regenerateIdentity:=Function(manager,user) user.GenerateUserIdentityAsync(manager))},.LoginPath = New PathString("/Account/Login")})
Protected Sub ChangePassword_Click(sender As Object,e As EventArgs)
If IsValid Then
Dim manager = Context.GetowinContext().GetUserManager(Of ApplicationUserManager)()
Dim signInManager = Context.GetowinContext().Get(Of ApplicationSignInManager)()
Dim result As IdentityResult = manager.ChangePassword(User.Identity.GetUserId(),CurrentPassword.Text,NewPassword.Text)
If result.Succeeded Then
Dim userInfo = manager.FindById(User.Identity.GetUserId())
signInManager.SignIn(userInfo,isPersistent:=False,rememberbrowser:=False)
Response.Redirect("~/Profile/Manage?m=ChangePwdSuccess")
Else
AddErrors(result)
End If
End If
End Sub
解决方法
事实证明您需要注销用户然后重新登录:
jstat
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。