I have a custom user role class with CompanyId
as a foreign key. So I could create user roles relative to the company. That means Administrator role or any other role per se, can be in multiple companies. I cannot go for a many-to-many relationship with Company and Role because claims of Administrator for one company may vary for another. So I have added the company Id in user role entity.
My problem is, user manager takes role name as a parameter in AddToRoleAsync()
, I cannot use this since the role name could be redundant. If I'm able to add user role by Id, that would be great. Is there a workaround to achieve this?
My current workaround is to add the company name as a prefix to role name. Like: ABC - Administrator; XYZ - Administrator. I don't like this very much, so won't the users.
If I could create a an overload method for both AddToRoleAsync()
and IsInRoleAync()
to look for role by it's ID and not by name, It would be great.
You can extend the
UserManager
class and add another method to assign a role to a user byRoleId
:Then register you own
UserManager
:And finally: