UserManager 的 ResetPasswordAsync 方法的详细信息

Details of ResetPasswordAsync method of UserManager

我想知道.net的ResetPasswordAsync()方法内部是如何工作的,试了很多都找不到相关文章,求助。

下面是 ResetPasswordAsync()

的代码
public virtual async Task<IdentityResult> ResetPasswordAsync(TUser user, string token, string newPassword)
    {
        ThrowIfDisposed();
        if (user == null)
        {
            throw new ArgumentNullException(nameof(user));
        }

        // Make sure the token is valid and the stamp matches
        if (!await VerifyUserTokenAsync(user, Options.Tokens.PasswordResetTokenProvider, ResetPasswordTokenPurpose, token))
        {
            return IdentityResult.Failed(ErrorDescriber.InvalidToken());
        }
        var result = await UpdatePasswordHash(user, newPassword, validatePassword: true);
        if (!result.Succeeded)
        {
            return result;
        }
        return await UpdateUserAsync(user);
    }

如果用户点击了忘记密码,那么您需要检查用户是否存在,然后生成用于密码重置的令牌密钥,如下所示

var callbackUrl = Url.Action("ResetPassword", "Account", 
new { UserId = user.Id, code = code }, protocol: Request.Url.Scheme);
    await UserManager.SendEmailAsync(user.Id, "Reset Password", 
"Please reset your password by clicking here: <a href=\"" + callbackUrl + "\">link</a>");    

然后发送如上所示的恢复邮件或发送到您自己的视图进行恢复。

代码取自微软GitHub账户