Introduction
In this blog, I am going to teach you how to implement 2FA authentication in your ASP.NET web application using Google Authenticator app. We need to install the Google Authenticator app on our mobile phone from the respective play store or app store, and using the QR code generated in the web application we will verify the user. When a user signs in to the web application, they need to enter a six-digit passcode that will be generated in the mobile app to finish two-factor authentication process. The passcode generated in the mobile app will be unique to the UserIdentifier and is a time-based one-time password (TOTP); i.e., it will expire after a certain interval of time.
Prerequisites
- Install the latest version of Visual Studio 2019 Community Edition from here.
- Install the Google authenticator app from Google Play Store/Apple App Store/Windows App Store
Steps for setup two-factor authentication in ASP.NET
Step 1
Create new empty project in ASP.NET
Step 2
Add the Webform name as Authenticator.aspx
Step 3
Then install the Google.Authenticator library from Nuget.
Step 4
Copy & paste the below mention designer & code behind file.
Step 5
Rebuild the solution and hit F5
Authenticator Designer Code
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Authenticator.aspx.cs" Inherits="_2Fa_4_5_1.Authenticator" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <link href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="container">
- <div class="jumbotron">
- <h2 class="text-info text-center">Google Authenticator</h2>
- <hr />
- <div class="row">
- <div class="col-md-12">
- <div class="section">
- <h3 class="text-info">Step 1: Install Google Authenticator
- </h3>
- <p>Please download and install Google Authenticator on your IPhone/IPad/Android device, if already not installed.</p>
- </div>
- </div>
- <div class="col-md-12">
- <div class="section">
- <h3 class="text-info">Step 2: Link your device to your account:
- </h3>
- <p>You have two options to link your device to your account:</p>
- <p><b>Using QR Code:</b> Select<b> Scan a barcode</b>. If the Authenticator app cannot locate a barcode scanner app on your mobile device, you might be prompted to download and install one. If you want to install a barcode scanner app so you can complete the setup process, select Install, then go through the installation process. Once the app is installed, reopen Google Authenticator, then point your camera at the QR code on your computer screen.</p>
- <p>
- <b>Using Secret Key:</b>
- Select <b>Enter provided key</b>, then enter account name of your account in the <b>"Enter account name"</b> box. Next, enter the secret key appear on your computer screen in the <b>"Enter your key"</b> box. Make sure you've chosen to make the key Time based, then select Add.
- </p>
- </div>
- </div>
- <div class="col-md-12">
- <div class="col-md-4">
- <asp:Image ID="imgQrCode" runat="server" />
- </div>
- <div class="col-md-6">
- <div>
- <span style="font-weight: bold; font-size: 14px;">Account Name:</span>
- </div>
- <div>
- <asp:Label runat="server" ID="lblAccountName"></asp:Label>
- </div>
- <div>
- <span style="font-weight: bold; font-size: 14px;">Secret Key:</span>
- </div>
- <div>
- <asp:Label runat="server" ID="lblManualSetupCode"></asp:Label>
- </div>
- </div>
- </div>
- <div class="clearfix"></div>
- <div class="col-md-12" style="margin-top: 2%">
- <div class="form-group col-md-4">
- <asp:TextBox runat="server" CssClass="form-control" ID="txtSecurityCode" MaxLength="50" ToolTip="Please enter security code you get on your authenticator application">
- </asp:TextBox>
- </div>
- <asp:Button ID="btnValidate" OnClick="btnValidate_Click" CssClass="btn btn-primary" runat="server" Text="Validate" />
- </div>
- <h3>Result:</h3>
- <div class="alert alert-success col-md-12" runat="server" role="alert">
- <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
- </div>
- </div>
- </div>
- </div>
- </form>
- </body>
- </html>





PArdeep SharmaPosted Dec 14, 2022, 9:57 AM
Their is bug when you entered previous code its says valid
tien tranPosted Sep 22, 2022, 8:18 AM
Where is you attachment file ?
jay singhPosted Jun 29, 2022, 3:50 AM
Thank you so much for this solution. but it always gives Invalid Code. issue.
Jd TechPosted May 26, 2021, 3:27 AM
Even the code was expired, it's still returning true. How to fix this?
Carlton MinterPosted Mar 26, 2021, 4:29 PM
6 digit code always invalid. Please help
Markus JohanssonPosted Feb 1, 2021, 12:10 PM
"You can also download the source code in the attachment."... but where is the attachment? =/
Omi BPosted Nov 2, 2020, 11:28 PM
Every time they said to scan QR code
Serge BroosPosted Sep 29, 2020, 9:38 AM
Dear Sir,That's exactly what I am looking for. But is there also a vb.code example. I tried to translate it, bit it does not work. And a second question, can it be done also withe the same code???? for example GIZWKNJWGAYGENDB and if I store that code in a session (linked to a user) It would help me enormously if these athings may work.
Jes SiePosted Sep 10, 2020, 11:07 PM
I already installed Google.Authenticator from nugget but I cannot reference Google.Authenticator. It gives me an error. What to do?
sanjaka malindaPosted Aug 18, 2020, 6:15 PM
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator(); string UserUniqueKey = user.UserName + GuruFrameworkConfig.Load().Google2FAKey; var setupInfo = tfa.GenerateSetupCode(GuruFrameworkConfig.Load().Google2FAIssuer, user.UserName, UserUniqueKey, false, 3);
Naman SharmaPosted Apr 8, 2020, 7:01 AM
I am getting error in line number 93. Can not convert int to bool. Can you help?
Naveen JoshiPosted Apr 1, 2020, 11:46 AM
I am getting error in line number 93. Can not convert int to bool. Can you help?
Ankush AgnihotriPosted Apr 1, 2020, 5:32 AM
Source code is all you read in the article
Bryar FatahPosted Oct 14, 2019, 8:34 AM
Hello how can download the source code