以下是我用来实现密码更改的代码。我正在遵循Asp>网络应用程序模板中的Manage.aspx页面中的模式来更改密码。
使用该方法不会散列密码,这是奇怪的,因为注册会散列它。所以,我添加了密码。问题是IdentityResult每次都返回false,即使这三个参数是正确的。每一行代码都会产生正确的结果,直到这一行,每次都产生错误
UPDATE: ChangePassword方法中的usr.ID是罪魁祸首。传入的用户名是users表中的ONLY条目,但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