Unable to add Token Providers when using multiple Identity Types

T-Rev 0 Reputation points
2023-02-03T22:53:31.64+00:00

My application has two separate identity types. I created two types, as opposed to simply using roles, because the information I have to save about the types are pretty drastically different, and they use two separate applications that act on the same data / database. Plus, these types "interact" with each other. It's sort of like a Business vs Consumer relationship.

I register them like this:

services.AddIdentityCore<UserTypeA>(o =>
{
o.Password.RequireDigit = true;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 10;
o.User.RequireUniqueEmail = false;
})
.AddRoles<ApplicationRole>()

services.AddIdentityCore<UserTypeB>(o =>
{
    o.Password.RequireDigit = true;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
    o.Password.RequireNonAlphanumeric = false;
    o.Password.RequiredLength = 10;
    o.User.RequireUniqueEmail = false;
})
.AddRoles<ApplicationRole>()

I don't know if this is the best way to do this, but it's been working fine since this project started. That said, I'm now trying to implement the password reset feature, and I keep getting this error:

"No IUserTwoFactorTokenProvider named 'Default' is registered."

I can add "AddDefaultTokenProviders", but I can't use it on both types. If I put it on both types, I get the same error. If I only put it on one, I can get a token, but only for the type that I've added "AddDefaultTokenProviders" to.

I've looked at the code for AddDefaultTokenProviders and I've seen that it's actually just calling AddTokenProvider. I've tried to implement that directly as well.

I've tried creating a base class that both of my Identity types inherit from and used the services.AddIdentity method to add it to the app and tried to just have one call to AddDefaultTokenProviders. Same issue.

I do feel like there's a pretty good chance that my issues are caused by a flawed design and my use of two identity types in general, but it's been working so far, and I don't know where to go from here.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
608 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,148 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,218 questions
{count} votes