In this article, I'll explain how to create an ASP.NET MVC web application with two-factor authentication using Google Authenticator to decrease the possibility of account takeovers and boost user and account security.
As it authenticates users using two possible authentication factors, two-factor authentication is a wonderful technique to help secure user accounts.
In this article, we are not taking user id and password from database but using static data. Here we only learn about Two-Factor Authentication using Google Authenticator.
Two-Factor Authentication using Google Authenticator in Asp.Net MVC
Step 1
Create a new project in Visual Studio, select an ASP.NET Web Application (.Net Framework), and press the Next button.
Step 2
Give the project a name, select the save location folder, and click on the Create button.
Step 3
Select MVC Option for project type and click on the Create button. Now your project is created.
Step 4
Add the Google.Authenticator package from NuGet.
Go to Tools Menu - NuGet Package Manager - Manage NuGetPackages For Solution - Search for Google.Authenticator > Install > Close.
Step 5
Add the Model class LoginModel.cs under Model Folder for login details that stores the username and password.
Step 6
Add the Google Authentication Private Key In Web.Config File under <appSettings>.
This key is not fixed; you can create it yourself with any character combination.
Any character combination use as private key in google authenticator.
Step 7
Create a Login action method in your controller; in my case, it's HomeController.
Step 8
Right click on the Login action method and select Add View Option, and then select the following: Then click the Add Button button to add a view for this action method.
Now a new file is created under View > Home > Login.cshtml.
Step 9
Add the following code into Login.cshtml:
Step 10
Create a Post type Login Action method in your controller and add the following code:
Step 11
Create the ConvertSecretToBytes function in the controller.
Step 12
Create a TwoFactorAuthenticate Action action method for your controller.
Step 13
Create a Logoff Action Method in your controller to handle the logout action.
Step 14
To handle an unauthorised login, add the following condition to all of your action methods, which in my case are three method.
Final HomeController.cs File
Step 15
All pages display the user's name and the option to log out. So add that code to the _Layout.cshtml page, and your _Layout.cshtml should now look like this:
Step 16
Display the username on the index page. So, add the following code to your index.cshtml page, and your index.cshtml should now look like this:
You can design your page yourself and according to your needs, but I've used default content, only adding a username to understand the concept.
Step 17
Now run your project.
When you run the project, by default, the index method will invoke but not authorize, so it will redirect to the login page.
Enter "Admin" as the user name and "12345" as the password, then click the login button.
Step 18
After clicking the login button, a QR code and the setup key for two-factor authentication were generated, which will be used later on the mobile app for code generation.
Step 19
Install the Google Authenticator app from the Play Store or here on your mobile device, and open it; you have two options: scan a QR code or enter a setup key.
We scan the QR code here, so you only have one Authenticator Code, which will change after a while. Enter this code on your website.
Step 20
If your code is correct, you will be redirected to your index page after clicking the submit button in the above screen.
Summary
As you can see, the implementation of Google Authenticator in ASP.Net MVC is very simple.
In this article, we learned about Two-Factor Authentication using Google Authenticator in Asp.Net MVC.
Thank you for reading my article. Please leave your comments in the comment box below.
No comments:
Post a Comment