How can I retrieve 2FA recovery codes for a user in ASP.NET Core Identity?

Viewed 1156

I'm currently working on an ASP.NET Core identity project. I need to create a page where the user can see the two-factor recovery codes that they have left to use. I've noticed that the recovery codes for a user is stored in the "ASPNetUserTokens" table in the default schema that ASP.NET Core Identity creates.

How can I retrieve the recovery codes for a user in my code?

I've had a look at the "GetAuthenticationTokenAsync" method on the UserManager class but was not sure how to go about using this.

1 Answers

I implemented it like this.

var internalLoginProvider = "[AspNetUserStore]";
var recoveryCodeTokenName = "RecoveryCodes";
var token = await _userManager.GetAuthenticationTokenAsync(user, internalLoginProvider, recoveryCodeTokenName);

Here are the links to the implementations and constants that I referred to.

InternalLoginProvider

RecoveryCodeTokenName

GetTokenAsync in CountCodeAsync

Related