asp.net - 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
解决方案
原来你需要注销用户,然后重新登录:
Protected Sub ChangePassword_Click(sender As Object, e As EventArgs)
If IsValid Then
Dim userManager = Context.GetOwinContext().GetUserManager(Of ApplicationUserManager)()
Dim signInManager = Context.GetOwinContext().Get(Of ApplicationSignInManager)()
Dim authenticateResult = Context.GetOwinContext().Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie).GetAwaiter.GetResult()
Dim accountChangeResult As IdentityResult = userManager.ChangePassword(User.Identity.GetUserId(), CurrentPassword.Text, NewPassword.Text)
If accountChangeResult.Succeeded Then
Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie)
Dim userInfo = userManager.FindById(User.Identity.GetUserId())
signInManager.SignIn(userInfo, isPersistent:=authenticateResult.Properties.IsPersistent, rememberBrowser:=False)
Response.Redirect("~/Profile/Manage?m=ChangePwdSuccess")
Else
AddErrors(accountChangeResult)
End If
End If
End Sub
推荐阅读
- javascript - contenteditable 的用途是什么,我可以将它用作编辑配置文件按钮吗
- javascript - Javascript 如果有类,则提示“是”,否则提示“否”
- php - 我可以在异常处理程序中访问环境变量吗
- python - 数据未使用 python mysql.connector 存储在 mysql 中
- python - 使用 Scikit-learn KMeans 对多维数组进行聚类
- angular - 如何防止复选框在将其变量设置为真或假时选择全部?
- java - 设置列表视图的背景颜色并在滚动时保存
- javascript - Selenium webdriver javaexecutor 与 for 循环
- octave - 如何将图像放置在八度音阶的按钮或面板上?
- javascript - 如何将本地json文件加载到javascript文件中