ASP.NET Core 3.1 - Password Recovery

Ken Haggerty
Created 02/20/2021 - Updated 02/23/2021 23:37

This article will describe the implementation of an automated password reset process for a user. I will assume you have downloaded the ASP.NET Core 3.1 - Users Without Identity Project or created a new ASP.NET Core 3.1 Razor Pages project. See Tutorial: Get started with Razor Pages in ASP.NET Core. You should review the earlier articles of the Users Without Identity Project series.

Users Without Identity Project and Article Series

UWIP v2 implements a forgot password process. The user can request an email sent to the confirmed email address.

Forgot Password Link
Forgot Password Link Mobile

The user must provide their confirmed email address on the forgot password page.

Forgot Password
Forgot Password Mobile

If the email address is found and confirmed, an email is sent with a link to the reset password page. The link includes a PasswordReset token based on the AppUser's SecurityStamp and provided by the TokenService.

Pages > Account > ForgotPassword > OnPostAsync:
var code = await _tokenService.GeneratePasswordResetTokenAsync(appUser).ConfigureAwait(false);
var callbackUrl = Url.Page(
    pageHandler: null,
    values: new { code },
    protocol: Request.Scheme);

var subject = "Reset password.";
var htmlMessage = "Please reset your password by " +
                            $"<a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.";                
var textMessage = "Please reset your password by coping and pasting this link: \r\n \r\n" +
                            $"{callbackUrl} \r\n \r\nto the address bar of your browser.";
await _emailSender.SendEmailAsync(Email, subject, textMessage, htmlMessage);

return RedirectToPage("./ForgotPasswordConfirmation");
Reset Email Sent
Reset Email Sent Mobile

The reset password page uses the email address to query the user. If the token is valid, the new password is hashed and the AppUser's PasswordHash is updated.

Pages > Account > ResetPassword > OnPostAsync:
var appUser = await _userService.GetAppUserByEmailAsync(Input.Email).ConfigureAwait(false);
// Don't reveal that the user does not exist
if (appUser == null) return RedirectToPage("./ResetPasswordConfirmation");

if (await _tokenService.ValidatePasswordResetTokenAsync(appUser, Input.Code).ConfigureAwait(false))
    if (!await _userService.UpdateAppUserPasswordAsync(appUser.Id, Input.Password).ConfigureAwait(false))
        throw new InvalidOperationException($"An error occurred resetting an AppUser password.");
    ModelState.AddModelError(string.Empty, "Invalid or expired code.");
    ShowInvalid = true;
    return Page();

return RedirectToPage("./ResetPasswordConfirmation");
Reset Password
Reset Password Mobile

If the token is invalid, the ResetPassword page uses the ShowInvalid property to display a "Try again?" link which redirects to the forgot password page.

Invalid Token
Invalid Token Mobile
Update 02/23/2021

I updated the article links.

Comment Count = 0

Please log in to comment.

Login Register
Logged in users receive web notifications.
Web Notifications