Tuesday, November 6, 2018

Google Authenticator siteweb visual studio


Google 2FA with authenticator

Package
https://www.nuget.org/packages/GoogleAuthenticator/

Nuget console in visual studio:
Install-Package GoogleAuthenticator -Version 1.2.1

Source of Google.Authenticator.dll:
https://github.com/brandonpotter/GoogleAuthenticator

------------------ code visual studio.net c#----------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Security.Cryptography;
using System.Text;
using System.Web.Profile;
using System.Web.Security;
using Google.Authenticator;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ////////////////////////////////////////////////
        // if user do not have a code, we generate one
        ////////////////////////////////////////////////
     


        ////////////////////////////////////////
        // get qrcode
        ////////////////////////////////////////
        TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();

        //the width and height of the Qr Code in pixels
        var setupInfo = tfa.GenerateSetupCode("Name of the app", "More info ABout the App", "SuperSecretKeyGoesHere22", 300, 300);

        // assigning the Qr code information + URL to string
        string qrCodeImageUrl = setupInfo.QrCodeSetupImageUrl;
        // show the Manual Entry Key for the users that don't have app or phone
        string manualEntrySetupCode = setupInfo.ManualEntryKey;

        // showing the qr code on the page "linking the string to image element"
        Image1.ImageUrl = qrCodeImageUrl;

        // showing the manual Entry setup code for the users
        Label1.Text = manualEntrySetupCode;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        // login
        string login01 = txtlogin01.Text.Trim();
        string password01 = txtpassword01.Text.Trim();
        string passcode01 = txtpasscode01.Text.Trim();

        // check sql if the user have a passcodekey01



        ////////////////////////////////////////
        // validate google code
        ////////////////////////////////////////
     
        TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
        bool isCorrectPIN = tfa.ValidateTwoFactorPIN("SuperSecretKeyGoesHere22", passcode01);
        if (isCorrectPIN == true)
        {
            Label2.Text = "i am cool";
        }
        else
        {
            Label2.Text = "i am Fool";
        }

    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}