1

I'm implementing 2FA using TOTP on my asp.net web api 2 webservice (NOT .net core).

The implementation uses the TotpSecurityStampBasedTokenProvider which is provided by the Microsoft.AspNet.Identity framework.

When looking at Google Authenticator requirements it lists that the secret needs to be a base32 encoded string.

If you look at the code from the TotpSecurityStampBasedTokenProvider it uses the user's SecurityStamp as the secret which is generated by the Identity Framework. This is a GUID and not a base32 encoded string.

According to the specifications for the Google Authenticator there is a requirement that the secret is a base32 encoded string.

How can I use the TotpSecurityStampBasedTokenProvider for use with Google Authenticator?

Peter
  • 14,221
  • 15
  • 70
  • 110

2 Answers2

3

The problem is that the TotpSecurityStampBasedTokenProvider provided by Microsoft.AspNet.Identity.Core implements a hardcoded timestep of 3 minutes. Google Authenticator uses a default value of 30 seconds, which can NOT be modified according to the documentation provided.

This causes different codes to be generated by both TOTP computations resulting in an always false authentication.

I have added a ticket to the github repository of aspnet.identity about this.

So for now nothing left for me to do than create my own totpProvider

Peter
  • 14,221
  • 15
  • 70
  • 110
2

The TotpSecurityStampBasedTokenProvider uses UserManager.CreateSecurityTokenAsync to generate the secret code which is used to calculate the token.

The code relies on the user ID, and is generated into a byte array, which is usually shown as base-32 or QR code on-screen to the user in the TOTP setup phase.

So long story short, there is nothing to worry about.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thank you Patrick, can you tell me which method I need to use on the UserManager to get the secret as a byte[] to encode into base-32 for the setup phase? I was using UserManager.GetSecurityStampAsync but that must be where my implementation is wrong. – Peter Nov 28 '18 at 09:42
  • That would be `GetSecurityStampAsync` or `CreateSecurityTokenAsync` probably. – Patrick Hofman Nov 28 '18 at 09:45
  • 1
    You're right, I was encoding the secret to a byte array using the incorrect encoding . I should use unicode. Thank you for the insight! – Peter Nov 28 '18 at 09:50
  • Yes, indeed. Good you found it. – Patrick Hofman Nov 28 '18 at 09:51
  • 1
    The sample projects are usually quite useful. See for example here: https://github.com/aspnet/Identity/blob/master/samples/IdentitySample.Mvc/Controllers/AccountController.cs – Patrick Hofman Nov 28 '18 at 09:51