Two-factor Authentication Support using Google Authenticator





4.00/5 (3 votes)
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.
This string
is actually Base32
encoding of real secret code. So you should do something like this:
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.