Search code examples
c#asp.net-mvcasp.net-identityrole-base-authorization

multiple roles for a user in aspnet identity


in my project, I have a user with 2 position, so, I have added a user with 2 roles in AspNetUserRoles table in my database,

so, for example, my user with this username : "[email protected]" has 2 roles: "accountant" and "ExecutiveExpert"

but when I call User.IsInRole("ExecutiveExpert") function, it just check the first role : accountant, not all the roles (accountant and ExecutiveExpert) and it returns false,

how can I change the asp.net Identity to check all the user roles? is it possible generally ?

these are my tables :

[AspNetUsers]:ID: c35721e2-05ef-4c32-8915-b4ad117c5a98  

[AspNetRoles]:    
Id                                      Name    
743f5c09-2b94-4da6-ab1c-9c9a6c9373b7    accountant    
845efdf6-ab07-475c-9eb1-b14365b1a54c    ExecutiveExpert

[AspNetUserRoles]:    
userID                                  RoleID    
c35721e2-05ef-4c32-8915-b4ad117c5a98    743f5c09-2b94-4da6-ab1c-9c9a6c9373b7    
c35721e2-05ef-4c32-8915-b4ad117c5a98    845efdf6-ab07-475c-9eb1-b14365b1a54c

Solution

  • In this case, the security stamp is updated. You need specifically renew/reset the security stamp

    UserManager.UpdateSecurityStampAsync(userId);
    

    This will help to fix your issue.