0

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@something.com" 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
R. Richards
  • 24,603
  • 10
  • 64
  • 64
Sepehr Estaki
  • 331
  • 5
  • 19
  • I don't get what you're saying. `User.IsInRole` will just give you a boolean. If it has the specified role, you'll get true, otherwise false. – mason Jun 01 '17 at 18:49
  • You need to pass a `string` to `IsInRole`. See [the docs](https://msdn.microsoft.com/en-us/library/system.web.security.roleprincipal.isinrole(v=vs.110).aspx) – Aluan Haddad Jun 01 '17 at 18:50
  • @mason: I just edit my question, would you please read it again? thank you. – Sepehr Estaki Jun 01 '17 at 18:55
  • I still don't get what you're asking. IsInRole only checks one role at a time. If you want to check two, you'd do something like `bool hasBothRoles = User.IsInRole("Role1") && User.IsInRole ("Role2");` If `IsInrole` is reporting the wrong thing for a single role, then you've got a completely different issue. – mason Jun 01 '17 at 18:58
  • @mason: my problem is that this user does not have **ExecutiveExpert** role, but I added this role in database for him,: in AspNetUserRoles table , it has **accountant** and **ExecutiveExpert** role – Sepehr Estaki Jun 01 '17 at 19:04
  • You would need to show the contents of your user tables, your roles table, and your relationship table. – mason Jun 01 '17 at 19:09
  • @mason: I edited my question again and show my content – Sepehr Estaki Jun 01 '17 at 19:22
  • Possible duplicate of [ASP.NET Identity check user roles is not working](https://stackoverflow.com/questions/20132795/asp-net-identity-check-user-roles-is-not-working) – Tieson T. Jun 02 '17 at 04:01

1 Answers1

2

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.

hongguan
  • 520
  • 2
  • 12