How to integrate Google Authenticator in ASP.Net MVC Project?

Tudip Technologies
2 min readFeb 16, 2022

What is Google Authenticator?

Google Authenticator is an OpenSource mobile security Application which is based on the two-factor Authentication (2FA). Basically, GoogleAuthenticator is useful to verify the user identity before providing them the access to a certain website or Application. It helps in protecting our accounts against password thefts.

How does it work?

The below image shows how the Google Authenticator actually works using two-factor authentication. While logging in the application, it provides an extra layer of authentication apart from the User Credentials. For login, user is asked to enter the Google Authenticator Code which is generated from its mobile App after scanning the QR Code or manually entering the setup key. The user is granted access of the application only after successful verification.

Steps to implement Google Authenticator:

1: Create A New Project to implement 2-FA

As shown below, we need to create a project of type ASP.NET Web Application. After clicking on the ‘OK’ button, we need to select the ‘Empty’ template and check the MVC checkbox.

2: Add NuGet Package for the Google Authenticator

We need to go to the Solution Explorer, Right Click on the References and select the Manage Nuget Packages option. After that, we need to search ‘Google Authenticator’ and install it.

3: Add a new ViewModel (class).

As we can see, we have added a new class “UserLoginModel.cs” in the application. We have created a folder named “ViewModel” for this and added this class file into this folder.

public class UserLoginModel
{
public string Username { get; set; }//Field to store the UserName public string Password { get; set; }//Field to store the PasswordName}

4: Create a Controller

For creating a MVC Controller, we need to go to Solution Explorer, right click on the ‘Controllers’ folder. Then we need to ‘Add Controller’, select Empty MVC Controller and name it. We are creating a controller named ‘HomeController’.

Read more: https://tudip.com/blog-post/how-to-integrate-google-authenticator-in-asp-net-mvc-project/

--

--