Use below code.
HTML
<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>
C#
using Google.Authenticator;
namespace _2Fa_4_5_1
{
public partial class Authenticator : System.Web.UI.Page
{
String AuthenticationCode
{
get
{
if (ViewState["AuthenticationCode"] != null)
return ViewState["AuthenticationCode"].ToString().Trim();
return String.Empty;
}
set
{
ViewState["AuthenticationCode"] = value.Trim();
}
}
String AuthenticationTitle
{
get
{
return "Ankush";
}
}
String AuthenticationBarCodeImage
{
get;
set;
}
String AuthenticationManualCode
{
get;
set;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lblResult.Text = String.Empty;
lblResult.Visible = false;
GenerateTwoFactorAuthentication();
imgQrCode.ImageUrl = AuthenticationBarCodeImage;
lblManualSetupCode.Text = AuthenticationManualCode;
lblAccountName.Text = AuthenticationTitle;
}
}
protected void btnValidate_Click(object sender, EventArgs e)
{
String pin = txtSecurityCode.Text.Trim();
Boolean status = ValidateTwoFactorPIN(pin);
if (status)
{
lblResult.Visible = true;
lblResult.Text = "Code Successfully Verified.";
}
else
{
lblResult.Visible = true;
lblResult.Text = "Invalid Code.";
}
}
public Boolean ValidateTwoFactorPIN(String pin)
{
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
return tfa.ValidateTwoFactorPIN(AuthenticationCode, pin);
}
public Boolean GenerateTwoFactorAuthentication()
{
Guid guid = Guid.NewGuid();
String uniqueUserKey = Convert.ToString(guid).Replace("-", "").Substring(0, 10);
AuthenticationCode = uniqueUserKey;
Dictionary<String, String> result = new Dictionary<String, String>();
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
var setupInfo = tfa.GenerateSetupCode("Complio", AuthenticationTitle, AuthenticationCode, 30, 30);
if (setupInfo != null)
{
AuthenticationBarCodeImage = setupInfo.QrCodeSetupImageUrl;
AuthenticationManualCode = setupInfo.ManualEntryKey;
return true;
}
return false;
}
}
}
VB.Net
Imports Google.Authenticator
Public Class Authenticator
Inherits System.Web.UI.Page
Private Property AuthenticationCode As String
Get
If ViewState("AuthenticationCode") IsNot Nothing Then
Return ViewState("AuthenticationCode").ToString().Trim()
End If
Return String.Empty
End Get
Set(ByVal value As String)
ViewState("AuthenticationCode") = value.Trim()
End Set
End Property
Private ReadOnly Property AuthenticationTitle As String
Get
Return "Ankush"
End Get
End Property
Private Property AuthenticationBarCodeImage As String
Private Property AuthenticationManualCode As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
lblResult.Text = String.Empty
lblResult.Visible = False
GenerateTwoFactorAuthentication()
imgQrCode.ImageUrl = AuthenticationBarCodeImage
lblManualSetupCode.Text = AuthenticationManualCode
lblAccountName.Text = AuthenticationTitle
End If
End Sub
Protected Sub btnValidate_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim pin As String = txtSecurityCode.Text.Trim()
Dim status As Boolean = ValidateTwoFactorPIN(pin)
If status Then
lblResult.Visible = True
lblResult.Text = "Code Successfully Verified."
Else
lblResult.Visible = True
lblResult.Text = "Invalid Code."
End If
End Sub
Public Function ValidateTwoFactorPIN(ByVal pin As String) As Boolean
Dim tfa As TwoFactorAuthenticator = New TwoFactorAuthenticator()
Return tfa.ValidateTwoFactorPIN(AuthenticationCode, pin)
End Function
Public Function GenerateTwoFactorAuthentication() As Boolean
Dim guid As Guid = Guid.NewGuid()
Dim uniqueUserKey As String = Convert.ToString(guid).Replace("-", "").Substring(0, 10)
AuthenticationCode = uniqueUserKey
Dim result As Dictionary(Of String, String) = New Dictionary(Of String, String)()
Dim tfa As TwoFactorAuthenticator = New TwoFactorAuthenticator()
Dim setupInfo = tfa.GenerateSetupCode("Complio", AuthenticationTitle, AuthenticationCode, 30, 30)
If setupInfo IsNot Nothing Then
AuthenticationBarCodeImage = setupInfo.QrCodeSetupImageUrl
AuthenticationManualCode = setupInfo.ManualEntryKey
Return True
End If
Return False
End Function
End Class