更新Asp.net Core 2身份中的密码

时间:2018-12-08 10:45:53

标签: asp.net-core asp.net-identity

这是我的登录IAction

控制器

    [HttpGet]
    [AllowAnonymous]
    public async Task<IActionResult> Login(string returnUrl = null)
    {
        // Clear the existing external cookie to ensure a clean login process
        await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

        ViewData["ReturnUrl"] = returnUrl;
        return View();
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
    {
        if (ModelState.IsValid)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);

            if (result.Succeeded)
            {

                _logger.LogInformation("User logged in.");
                return RedirectToLocal(returnUrl);
            }
            if (result.RequiresTwoFactor)
            {
                return RedirectToAction(nameof(LoginWith2fa), new { returnUrl, model.RememberMe });
            }
            if (result.IsLockedOut)
            {
                _logger.LogWarning("User account locked out.");
                return RedirectToAction(nameof(Lockout));
            }

            else
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return View(model);
            }
        }
        return View(model);
    }

现在我当前的电子邮件和密码是

电子邮件: john@gmail.com

密码: john@gmail.com123

,我希望将密码 john@gmail.com123 更新为 john@gmail.com677 请帮我解决这个问题, 预先感谢

1 个答案:

答案 0 :(得分:1)

看看UserManager.GeneratePasswordResetTokenAsyncUserManager.ResetPasswordAsync

如果您要处理的情况是使用“忘记密码”的密码,那么您很可能希望向用户发送带有链接的电子邮件。该链接会将用户带到采取将重置令牌作为查询参数的操作。然后,用户可以单击此链接以重置其密码,然后从那里进行ResetPasswordAsync呼叫。

如果这是您正在查看的情况(您的帖子不是特定的),则应在此处查看Microsoft指南:Enable account confirmation and password recovery

一些下限代码(仅用于说明/指导目的):

// Get the user by email - may need to be careful of casing here
IdentityUser user = _userManager.Users.First(x => x.Email == "email@example.com");

// Generate the reset token (this would generally be sent out as a query parameter as part of a 'reset' link in an email)
string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user);

// Use the reset token to verify the provenance of the reset request and reset the password.
IdentityResult updateResult = await _userManager.ResetPasswordAsync(user, resetToken, newPassword);