One of the common practices in user account management is to provide a possibility for the users to change their passwords if they forget it. The password reset process shouldn’t involve application administrators because the users themselves should be able to go through the entire process on their own. Usually, the user is provided with the Forgot Password link on the login page and that is going to be the case for this article as well.

So let’s explain how the Password Reset process should work in a nutshell.

A user clicks on the Forgot password link and gets directed to the view with the email field. After a user populates that field, an application sends a valid link to that email. An email owner clicks on the link and gets redirected to the reset password view with the generated token. After populating all the fields in the form, the application resets the password and the user gets redirected to the Login (or Home) page.

To download the source code for this project, you can visit the Reset Password with ASP.NET Core Identity repository.

To navigate through the entire series, visit the ASP.NET Core Identity series page.

Let’s look at the basic navigation for this article:

Including the Email Service into the Project

Sending an email from ASP.NET Core is not this article’s topic, therefore, we won’t be explaining that. If you are not familiar with the process, you can read the Send Email Message from ASP.NET Core article, where we explain that process in great detail (sync, async, attachments, different body types, etc).

In this project, we are going to reuse the email service we’ve created in that article.

So, to start things off, let’s add an existing project in our solution and add the reference to the main project:

added email service for Password Reset purpose

Next, we are going to add a configuration for the email service in the appsettings.json file:

And let’s register our Email Service in the Startup class in the ConfigurationServices method:

Finally, we have to inject this service in the Account controller:

Email service is prepared and ready to use. Therefore, we can move on.

Forgot Password Functionality

Let’s start with the ForgotPasswordModel class:

The Email property is the only one we require for the ForgotPassword view. Let’s continue by creating additional actions:

This is a familiar setup. The first action is just for the view creation, the second one is for the main logic and the last one just returns confirmation view. Of course, we have to create these views:

And another one:

If we want to navigate to the ForgotPassword view, we have to click on the forgot password link in the Login view. So, let’s add it there:

And test it:

Navigation for Reset Password

Now we can modify the POST action:

So, if the model is valid we get the user from the database by its email. If they don’t exist, we don’t create a message that the user with the provided email doesn’t exist in the database, but just redirect that user to the confirmation page.

This is a good practice for security reasons.

If they exist, we generate a token with the GeneratePasswordResetTokenAsync method and create a callback link to the action we are going to use for the reset logic. Finally, we send an email message to the provided email address and redirect the user to the ForgotPasswordConfirmation view.

With this setup, we are missing two important things. The token can’t be created and we don’t have the ResetPassword actions. So, let’s fix that.

Enabling Token Generation

We can’t create our token because we haven’t registered the token provider for our application at all. So to do that, we have to modify the ConfigureSerives method in the Startup class:

And that’s all it takes. The AddDefaultTokenProviders extension method will do exactly that, add the required token providers to enable the token generation in our project. But there is one more thing we have to configure.

What we want for our password reset token is to be valid for a limited time, for example, 2 hours. So to do that, we have to configure a token life span in the ConfigureServices method:

Excellent. We can move on.

Reset Password Functionality

Before we start with the ResetPassword actions, we have to create a ResetPasswordModel class:

Now, let’s create the required actions in the Account controller:

This is a similar setup as we had with the ForgotPassword actions. The HttpGet ResetPassword action will accept a request from the email, extract the token and email values, and create a view. The HttpPost ResetPassword action is here for the main logic. And the ResetPasswordConfirmation is just a helper action to create a view for a user to get some confirmation about the action.

With that in place, let’s create our views. First the ResetPassword view:

Pay attention that Email and Token are hidden fields, we already have these values.

After this view, we have to create one more for the confirmation:


Now we can modify the POST action:

So, the first two actions are the same as in the ForgotPassword action. We check the model validity and also if the user exists in the database. After that, we execute the password reset action with the ResetPasswordAsync method. If the action fails, we add errors to the model state and return a view. Otherwise, we just redirect the user to the confirmation page.

We can see this in practice:

Complete ForgotPassword Workflow

As you can see, everything works as expected, and we can confirm that by comparing two hashed passwords in the database:

Password comparing


We did an excellent job here. Everything is working as expected and we can sum this article up.

So, we have learned:

  • About injecting external email service into an existing project
  • How to implement forgot password functionality
  • The way to register token providers and to set up the life span of a token
  • How to implement reset password functionality

In the next article, we are going to talk about email confirmation during the registration process.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.