'Asp net core IdentityServer4 GetValidTwoFactorProvidersAsync returns null

I am building an identity server but I can't get the Two Factor Auth to work. I have a demo project where it works, but somewhere there is a 'magic happens' moment as far as I can see. The user logs in fine, then hits this code:

                //logged in ok, check for mfa
                if (user.TwoFactorEnabled)
                {
                    
                    //make sure it has sms as an option 
                    var providers = await _userManager.GetValidTwoFactorProvidersAsync(user);

                    if (!providers.Contains("Phone"))

But the Get valid two factors providers comes back null. I have a web site where it returns "Phone" and "Email", but I can't figure out where those are specifically set. In my Startup.cs page I am adding the sms sender like this:

services.AddTransient<ISmsSender, SmsSender>();

In asp.net identity (not core) I could specifically set them like this:

usermanager.RegisterTwoFactorProvider("SMS", new PhoneNumberTokenProvider<IdentityUser> {MessageFormat = "Code: {0}"});

I have tested my Twilio SMS class and it works, sends text just fine. But how do I register it? Any ideas would be appreciated.



Solution 1:[1]

I had this very problem when using AddIdentityCore<TUser> to get a lightweight identity registration (without default UI). Calling AddDefaultTokenProviders() on the resulting IIdentityBuilder populated the collection.

builder.Services.AddIdentityCore<MyUserType>().AddDefaultTokenProviders();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HCL