Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4292

Forgot password in aspnet identity

I have been following this article

The article doesn't appear to be complete.

This is the web API I'd create to generate the forgot password link as per the article.

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

As we can see the ResetPassword is the URL action that is supposed to be invoked when the link is clicked in our inbox. However the article doesn't give a method definition for ResetPassword API.

Upvotes: 0

Views: 4536

Answers (1)

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30545

there must be ResetPassword method inside your controller as an action method.

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

Source: Link

Upvotes: 3

Related Questions