忘记了aspnet身份的密码

时间:2019-01-20 17:43:34

标签: c# asp.net-web-api asp.net-identity

我一直在关注article

该文章似乎并不完整。

这是我根据文章创建的用于生成忘记密码链接的Web API。

   public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
            // Don't reveal that the user does not exist or is not confirmed
            return View("ForgotPasswordConfirmation");
        }

        var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
        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>");        
        return View("ForgotPasswordConfirmation");
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

如我们所见,ResetPassword是当在收件箱中单击链接时应该调用的URL操作。但是,本文没有提供ResetPassword API的方法定义。

1 个答案:

答案 0 :(得分:1)

控制器中必须有ResetPassword方法作为操作方法。

  //
  // GET: /Account/ResetPassword
  [AllowAnonymous]
  public ActionResult ResetPassword(string code)
  {
     return code == null ? View("Error") : View();
  }

  //
  // POST: /Account/ResetPassword
  [HttpPost]
  [AllowAnonymous]
  [ValidateAntiForgeryToken]
  public async Task<ActionResult> ResetPassword(ResetPasswordViewModel model)
  {
     if (!ModelState.IsValid)
     {
        return View(model);
     }

     var user = await UserManager.FindByNameAsync(model.Email);
     if (user == null)
     {
        // Don't reveal that the user does not exist
        return RedirectToAction("ResetPasswordConfirmation", "Account");
     }
     var result = await UserManager.ResetPasswordAsync(user.Id, model.Code, model.Password);
     if (result.Succeeded)
     {
        return RedirectToAction("ResetPasswordConfirmation", "Account");
     }
     AddErrors(result);
     return View();
  }

来源:Link