This is a walkthrough on how to implement Second Factor Authentication (2FA) using Google Authenticator in ASP.MVC with C# code.
Implementing authentication by only requesting a user name and password is not good enough to secure our web applications representing a high risk for people to steal our user’s password. A computer could run complex algorithms to attempt to calculate and decipher passwords in a very small amount of time. People are not as fast as computers especially when trying to infiltrate a system with strong cryptography.
Developers should attempt to implement at least two of the three known approaches for human authentication:
- Something the user knows: user names and passwords, questions related to user
- Something the user has: phone, flash drive, smart card
- Something the user is: finger prints and other biometrics
Google Authenticator helps us to implement Second Factor Authentication by adding an additional layer of security to our applications with something the user knows and something the user has.
- Create new empty ASP.NET MVC project
- Install Nuget package from Google Authenticator.
- Create model class containing the user name and password. I named it LoginModel
- Create an MVC Controller. Create a new ActionResult for the login page.
- Add a new View for the Login ActionResult created in step 4 and use the LoginModel created in step 3
- Create a new ActionResult for when the user is authenticated. Authorized users will be redirected after successful login
- Create a new View for ActionResult in step 6.
- Add ActionResult to verify Second Factor Authentication
- Setup your Login page created in step 5 as your startup page
- Run the application. Login and then use your Google Authenticator phone app to scan or manually type the QR Code shown in your web app.
- Go back to the web app and type the 6 digit token from your Google Authenticator app
I did not include the code in between the steps to keep it as simple as possible. The steps should be a guideline. You can apply the code that goes on each step from the source code.
However, I will paste the code that implements the Google Authenticator.
Once we have a valid user login and password, we use Google Authenticator to Generate the QR Code:
To understand the previous code, take a look the GenerateSetupCode signature:
Finally, this is piece of code used after the user has entered the token based on the QR Code received by Google Authenticator
This is how the MVC application should look:
We have covered just the basics using static data to login and interact with our user. Take this code and transform it into a more robust application. In the real world, we would need to interact with a database or service to validate our login. We would also need to consider how to interact with external applications or services once we have been authenticated. Please leave your comments with questions or suggestions and share your experience working with Second Factor Authentication.