更新Asp.net Core 2标识中的密码



这是我的登录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.com123john@gmail.com677请帮我解决这个问题,提前感谢

看看UserManager.GeneratePasswordResetTokenAsyncUserManager.ResetPasswordAsync

如果你正在处理的场景是"忘记密码",那么你很可能想向用户发送一封带有链接的电子邮件。此链接将用户带到一个操作,该操作将重置令牌作为查询参数。然后,用户可以单击此链接重置密码,并从此处进行ResetPasswordAsync调用。

如果这是你正在看的场景(你的帖子没有具体说明(,那么你应该看看这里的微软指南:启用帐户确认和密码恢复

一些精简代码(仅用于说明/教学目的(:

// 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);

相关内容

  • 没有找到相关文章

最新更新