以下是我用来更改密码的代码。我正在遵循 Asp>net Web 应用程序模板中的 Manage.aspx 页面中的模式来更改密码。
使用该方法不会对密码进行哈希处理,这很奇怪,因为注册会对其进行哈希处理。所以,我添加了密码哈希。问题是即使三个参数正确,IdentityResult 每次都返回 false。每个代码行都会产生正确的结果,直到这一行,每次都产生错误
更新: ChangePassword 方法中的 usr.ID 是罪魁祸首。传入的用户名是用户表中的唯一条目,但 usr.Id 与表中的用户 ID 不匹配。它甚至是如何检索 id 的?
Dim result As IdentityResult = manager.ChangePassword(usr.Id, currentPass, newhash)
这是方法
Private Sub btnSubmitPasswordChange_Click(sender As Object, e As EventArgs) Handles btnSubmitPasswordChange.Click
Dim db As New MySQLDatabase("MyConnString")
Dim ut As New UserTable(db)
Dim username As String = EncryptDecrypt.DecryptQueryString(Request.QueryString("rtu"))
Dim userId As String = ut.GetUserId(username)
Dim currentPass As String = ut.GetPasswordHash(userId)
Dim usr As New IdentityUser(username)
Dim manager = New UserManager()
manager.UserValidator = New UserValidator(Of IdentityUser)(manager) With {.AllowOnlyAlphanumericUserNames = False}
Dim phasher As New PasswordHasher
Dim newhash As String = phasher.HashPassword(Password.Text)
Dim result As IdentityResult = manager.ChangePassword(usr.Id, currentPass, newhash)
If result.Succeeded Then
Response.Redirect("~/Account/Login.aspx")
Else
lblResetSuccess.Text = "Password change failed!"
End If
Dim changed As Integer = ut.SetPasswordHash(userId, newhash)
End Sub