I am trying to understand how Google Two Factor Authenticator process works in order to incorporate it into my site. Its my understanding that there are two different part to the process
- A user on my site enable 2FA which created a link between my user and the app. This is a onetime step and does not happen on every login attempt.
- Every time the user logs in, he'll need to provide a six digit code from Google Authenticator app.
Now, the following code, generate the QR Image and the setup-code to enable the 2FA and link the account to Google Authenticate.
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
string accountSecretKey = Guid.NewGuid();
var setupInfo = tfa.GenerateSetupCode("Dotnet Awesome", login.Username, accountSecretKey, 300, 300);
ViewBag.BarcodeImageUrl = setupInfo.QrCodeSetupImageUrl;
ViewBag.SetupCode = setupInfo.ManualEntryKey;
Now on every request, I would authenticate the user using the following code
TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
tfa.ValidateTwoFactorPIN(accountSecretKey, "Six Digit Code");
Question
In the above code, will the accountSecretKey
code something I would have to save into my database so I can pass it every-time I want to validate? Or, the accountSecretKey
something I would have to re-create on every login attempt? If this code something I would store into my database, should it be encrypted like a password as well?