Dear dharmendr,
Thanks for the reply and I will certainly try to implement this code as well. At the moment I am struggling with an easier solution and the google authenticator. I got it working but unfortunately is is written again in C#. The example is what I was looking for; I tried to translate in with a program to vb.net but it does not work. May I ask you to look for a while to the code??? Perhaps You see immediately the problem. That would help me enormously.
I shall first give the working code in c# and then the vb.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>
C#
using Google.Authenticator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
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-code (if it would work)
Imports Google.Authenticator
Imports System
Imports System.Collections.Generic
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Public Class Authenticator
Inherits Web.UI.Page
Private Property AuthenticationCode As String
Get
If ViewState("AuthenticationCode") IsNot Nothing Then Return ViewState("AuthenticationCode").ToString().Trim()
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)
If Not Page.IsPostBack Then
lblResult.Text = String.Empty
lblResult.Visible = False
GenerateTwoFactorAuthentication()
imgQrCode.ImageUrl = AuthenticationBarCodeImage
lblManualSetupCode.Text = AuthenticationManualCode
lblAccountName.Text = AuthenticationTitle
Else
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