ASP.NET 身份更改密码使用户退出

时间:2021-03-22 22:16:02

标签: asp.net .net vb.net asp.net-identity change-password

我遇到了 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

1 个答案:

答案 0 :(得分:0)

事实证明您需要注销用户然后重新登录:

jstat
相关问题