Forgot Password with asp.net identity
<p>In my MVC and web API projects I'm using ASP.NET Identity for the Login.</p>
<p>This is my Forgot Password function on the AccountController:</p>
<pre class="lang-cs prettyprint-override"><code>[HttpPost]
[AllowAnonymous]
[Route(&quot;ForgotPassword&quot;)]
public async Task&lt;IHttpActionResult&gt; ForgotPassword(ForgotPasswordViewModel model) 
{
    if (ModelState.IsValid) 
    {
        var user = await UserManager.FindByNameAsync(model.Email);

        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id))) 
            return Ok();

        try 
        {
            var code = await UserManager.GeneratePasswordResetTokenAsync(user.Id);
            
            var callbackUrl = new Uri(@ &quot;http://MyProject/ResetPassword?userid=&quot; + user.Id + &quot;&amp;code=&quot; + code);

            string subject = &quot;Reset Password&quot;;
            string body = &quot;Please reset your password by clicking &lt;a href=\&quot;&quot; + callbackUrl + &quot;\&quot;&gt;here&lt;/a&gt;&quot;;
            SendEmail(user.Email, callbackUrl, subject, body);
        } 
        catch (Exception ex) 
            throw new Exception(ex.ToString());

        return Ok();
    }

    // If we got this far, something failed, redisplay form
    return BadRequest(ModelState);
}
</code></pre>
<p>I'm getting the confirmation email, I am successfully redirected to the Reset Password View but then I'm not sure how to proceed.</p>
<p>How do I reset a password with the following parameters: NewPssword, ConfirmPassword and Code?</p>
<p>I tried to call the <code>SetPassword</code> method but got a 401 error.</p>
<pre class="lang-js prettyprint-override"><code>self.resetPassword = function () {
    var data = {
        Email: self.loginUserName(),
        NewPassword: self.registerPassword(),
        ConfirmPassword: self.registerPassword2()
    }
    $.ajax({
        type: 'POST',
        url: '../API/Account/SetPassword',
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify(data),
        complete: showError
    });
}
</code></pre>
<p>And the setPassword function:</p>
<pre class="lang-cs prettyprint-override"><code>// POST api/Account/SetPassword
[Route(&quot;SetPassword&quot;)]
public async Task&lt;IHttpActionResult&gt; SetPassword(SetPasswordBindingModel model) 
{
    if (!ModelState.IsValid) 
        return BadRequest(ModelState);

    IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

    if (!result.Succeeded) 
        return GetErrorResult(result);

    return Ok();
}
</code></pre>
<p>Currently the process is a s follow:</p>
<ol>
<li>user clicks on &quot;Forgot Password&quot; link.</li>
<li>He is redirected to the Forgot Password View that has one field: Email.</li>
<li>The user provides his email address and submit the form.</li>
<li>the request is being sent to the Forgot Password function.</li>
<li>An email with a unique code is being sent to the client.</li>
</ol>
<p><em>Till here I wrote the code and got it to work but I'm not sure how to</em> implement the next steps:</p>
<ol start="6">
<li>The user is redirected the the Reset Password View that has 3 fields: Email, New Password and Confirm Password.</li>
<li>The user submits the form, a request is being sent to the SetPassword function (?) with the unique code and the user's password is being reset successfully.</li>
</ol>
<p>Thanks in advance.</p>