Question: AddUserManager() only accepting derived types?
I am trying to understand the design here as part of my learning of Identity.
This type check prevents using the OOTB UserManager with a custom User class that doesn't inherit from IdentityUser (the same applies to RoleManager): https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/IdentityBuilder.cs#L192
so if you have something like:
var identityBuilder = services.AddIdentity<DomainUser, DomainGroup>(); and then
identityBuilder.AddUserManager<UserManager<DomainUser>>();
You will hit the exception on this line: https://github.com/aspnet/Identity/blob/c8a276e716064ac40df5a56512e0217b14b48060/src/Microsoft.AspNetCore.Identity/IdentityBuilder.cs#L179
But, if you replace the UserManager with a custom AdUserManager:
identityBuilder.AddUserManager<AdUserManager<DomainUser>>();
Then line 179 will not throw. AdUserManager is nothing more than an empty class inheriting from UserManager.
Is this by design, because AddIdentity() by default registers the UserManager as a scoped class
I believe the intent is to make sure that when you register a custom UserManager or RoleManager type it will still be assignable to UserManager<TUser> or RoleManager<TRole> for whatever TUser and TRole types you are using.
If these checks were constraining the user and role types (and I am not sure they are) that would probably be a bug as those types should only be constrained by the stores, not the managers.
I am not sure but at first glance it seems that maybe we have a short-circuiting bug in the ifs, e.g. instead of:
var userManagerType = typeof(UserManager<>).MakeGenericType(UserType);
var customType = typeof(TUserManager);
if (userManagerType == customType ||
!userManagerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
{
throw new InvalidOperationException...
Maybe it should be:
var userManagerType = typeof(UserManager<>).MakeGenericType(UserType);
var customType = typeof(TUserManager);
if (userManagerType != customType &&
!userManagerType.GetTypeInfo().IsAssignableFrom(customType.GetTypeInfo()))
{
throw new InvalidOperationException...
@HaoK would know for sure.
I would also suggest that there's a unit test for this, because currently all written unit tests make use of derived UserManager classes.
I am happy to submit a PR, if it would be helpful, and we clarify the intent.
Totally agree that we should have a test for this check. A PR sounds like a good idea to me.
Yeah this is both a bug and by design :) AddIdentity always registers a UserManager and RoleManager already. This method is intended only if you want to add a derived manager. That said, @divega is right and the || should be an &&. If you want to submit a PR for this, we'd be glad to take it...
So looked at this code again, the current code/behavior is correct, this is the overload we added to hook up a delegate to return an alias to the normal scoped UserManager:
Services.AddScoped(customType, services => services.GetRequiredService(userManagerType));
So if you call this with the same type its an infinite loop... There were tests insuring that this would throw, which clue'd me in that this was intentional...go tests :)