Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / C#
Tip/Trick

Two-factor Authentication Support using Google Authenticator

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
11 Feb 2015CPOL 13.3K   11   1
One thing I should mention about writing two-factor authentication for work with Google Authenticator.

Introduction

Recently, my employers made all of us access our production servers using two-factor authentication. We were advised to use Google Authenticator program for generation of time-based passwords. Unfortunately my Smartphone is too old to be able to install it. So I had to use BlueStacks Android emulator to run this program.

In some time, I came across a great article here revealing how Google Authenticator actually works. In that moment, I decided to write my own program which should do the same thing (generate time-based passwords). I took implementation of GetPassword method from the article and passed there a secret code provided by our application. Unfortunately, my program kept generating different passwords from Google Authenticator.

Implementation

It appeared that secret code which is used by Google Authenticator should not be passed directly into GetPassword function from the article.

Image 1

This string is actually Base32 encoding of real secret code. So you should do something like this:

C#
var secret = Encoding.ASCII.GetString(Base32.Decode(EncodedSecretCode));
 
var timeBasedPassword = GetPassword(secret);

where EncodedSecretCode is this JFCGY43BOZZG6QTH.

You may find implementation for Base32 decoding/encoding here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Finstek
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun11-Feb-15 19:23
Humayun Kabir Mamun11-Feb-15 19:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.