【发布时间】:2018-03-12 09:30:12
【问题描述】:
我正在使用 ASP.NET 身份,并且我有基本的忘记密码/重置密码功能。
当您填写忘记密码的表单时,它会使用_userManager.GeneratePasswordResetTokenAsync(user) 创建一个重置令牌
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Email);
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user)))
{
return View("ForgotPasswordConfirmation");
}
var code = await _userManager.GeneratePasswordResetTokenAsync(user);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
await _emailSender.SendEmailAsync(model.Email, "Reset Password",
$"Please reset your password by clicking here: <a href='{callbackUrl}'>link</a>");
return View("ForgotPasswordConfirmation");
}
// If we got this far, something failed, redisplay form
return View(model);
}
我注意到重置密码页面的唯一验证是检查代码是否为空,而不是检查它是否仍然有效或未过期。
[HttpGet]
[AllowAnonymous]
public IActionResult ResetPassword(string code = null)
{
if (code == null)
{
throw new Exception("A code must be supplied for password reset.");
}
var model = new ResetPasswordViewModel { Code = code };
return View(model);
}
在您尝试重置密码并调用_userManager.ResetPasswordAsync(user, model.Code, model.Password)之前,它实际上不会检查令牌是否有效
我希望能够在他们点击“重置密码”页面以向用户显示消息时验证代码是否仍然有效,而不是在他们尝试重置密码之后。
有没有办法检查它是否有效?
【问题讨论】:
-
UserTokenProvider.ValidateAsync("ResetPassword", token, this, user))??? -
谢谢,我以为有可用的方法,只是找不到
-
什么是 UserTokenProvider 类? @steven:如果你能发布你的问题的解决方案,那就太好了?
标签: c# asp.net-core asp.net-identity