32

I am still confused about all this Identity stuff. 

First I am still confused the difference between Roles, Policies/Claims. From what I read roles is the old way of doing stuff and was kept for backward compatibility, so does that mean AspNetRoleClaims is part of this backward compatibility?

I think I understand Claims and Policies when thinking of them individual, like policy is basically a set of rules that must pass and gives the ability to change rules without having to go through out all the code and change roles.

Were a claim, is basically a trusted source is vouching for about that user(ie this is their age, which might come from a government source ).

Now what confuses me is putting it all together.

I generated the Identity tables and see

AspNetUsers
AspNetUserRoles
AspNetRoles
AspNetRoleClaims
AspNetUserClaims
AspNetUserLogins

I get what the AspNetUsers table does and AspNetUserLogins(seems to be if they use like external login providers).

I get confused on what the difference between AspNetRoleClaims and AspNetUserClaims.  Do I just use AspNetUserClaims or do I use everything?

Say I have this secenario

I have a company that has many branches, in each branch their will be an administrator of that branch, they got full power over the branch and can do anything but nothing at another branch. At the company level there will an administrator who can do anything at the company level and any branch. Finally I have a person in the branch who can just add new employees.

What does this all look like? Do I make 3 roles?

CompanyAdmin
BranchAdmin
AddUsersAtBranchLevel (or is this some sort of claim??)
What do the tables look like? Is there anything going to be in AspNetRoleClaims? AspNetUserClaims?

Now I can make a policy to check if the user is a branch admin and if they are trying to edit their branch?

Or do I just forget all the role stuff and have in the AspNetUserClaims

User1   CanAddUserToBranch true
User1 CanDeleteUserBranch true
User1 CanAddUserToCompany true

Then in my code make those all different "ClaimTypes" and create a polciy that sees if they have say "CanAddUserToBranch" and then another claim or policy to check what branch they are in to make sure they are trying to add something to the right branch?

Edit

Do you think I Need to use Resource-based authorization?

6
  • 4
    You had my upvote at "I am still confused about all this Identity stuff..." However, your question is perfect. As you said, according to several credible sources, we aren't supposed to use Roles any more (in a nutshell and there are 2.1.bugs for roles). Yet, the only current answer below recommends Roles. Most questions like your's on SO go unanswered or using Roles again... or antiquated code from older versions of Identity/Core/etc...or scaffolding out Identity and customizing it. I've been watching msdn, etc videos, reading articles and blogs all day and the answer is still not clear.
    – Sum None
    Jun 15, 2018 at 19:24
  • @SumNone I'd appreciate links to the credible sources you mention... Nothing wrong with using roles if it does the job. Roles are claims. You may also use IdentityCore, and create your own role claims in the AspNetUserClaims table. Choose whatever solves the problem the best! (please share links though, because I haven't heard a recommendation stating roles shouldn't be used.)
    – galdin
    Jan 11, 2019 at 19:09
  • 1
    Well the problem is if you don't understand one of the solutions it's hard to choose which solves the problem best. After much research I sort of just gave up and just did "Roles"(no role claims or any claims). Requirements of what was needed allowed me to just use Roles so I got lucky. I am finding the same thing as SunNone everyone recommends to not use Roles(just read it one of core books) but no one really goes into details on how to use them and if they do it is such a limited scenario that when I tried to apply it to my app I ran into issues fast.
    – chobo2
    Jan 11, 2019 at 19:16
  • 1
    github.com/aspnet/Docs/issues/7108 That is a link where Rick Anderson pulled my question out of one of their older code examples and made it a work item. When the latest version of Identity came out (at that time... I think 2.1), roles were basically deprecated. There was also a couple "Channel 9" videos (IIRC) that came out with blowdart where he specifically said it. Here's another link about it too: github.com/aspnet/Identity/issues/1813 I wish you asked me back in June, I could have been more exact and probably shown you where blowdart said that in the video.
    – Sum None
    Jan 13, 2019 at 5:39
  • @gldraphael Please see my previous comment (I forgot to reply to you). Also: channel9.msdn.com/Blogs/Seth-Juarez/… Here's the blowdart vid... It's great overall, but start watching at the 34 minute mark to hear about Roles. There is a second video too. Generally speaking, I'm with you. You're preaching to the choir. :) But, when starting new projects, I try to go with the latest standards and whatnot.
    – Sum None
    Jan 13, 2019 at 6:41

1 Answer 1

48
+25
+------------------+------------------+
|      Table       |   Description    |
+------------------+------------------+
| AspNetUsers      | The users.       |
| AspNetRoles      | The roles.       |
| AspNetUserRoles  | Roles of users.  |
| AspNetUserClaims | Claims by users. |
| AspNetRoleClaims | Claims by roles. |
+------------------+------------------+
  • A role is something assigned to a user.
    • Eg. Jane is an admin.
  • A claim is something claimed by a user.
    • Eg. Jane's date of birth is 1990-10-1.
  • A role-claim is a claim claimed by a role.
    • Eg. Admins have access to the dashboard.

If you find roles and claims confusing, it's probably because roles are a special case of claims i.e. roles are claims.

Role vs Policy

  • For role based authorization, the authorization system checks if the user has been assigned the roles required to access the given resource.

    • Eg: only users with the Admin role can access the dashboard.
  • For policy based authorization, some business logic is executed to decide if the resource access should be authorized.

    • Eg: only Admins with an age above 40 can access financial data.

Say I have this scenario

I have a company that has many branches, in each branch their will be an administrator of that branch, they got full power over the branch and can do anything but nothing at another branch. At the company level there will an administrator who can do anything at the company level and any branch. Finally I have a person in the branch who can just add new employees.

Here's one way of doing it:

2 roles: Admin, TheRoleThatCanAddUsers
A claim called Branch that can take a branch id (or anything else to identify the branch). Company admins can use a value like "CompanyWide" or 0 or -1.

Now create a policy that checks the Role and the Branch claim and decides if the user should be authorized.

14
  • So in your scenario are you using role claims? My understanding is you either use role/role claims or just user claims. How you wrote it, you seem to use all?
    – chobo2
    May 28, 2018 at 21:41
  • 1
    @chobo2 No I haven't used roleclaims here. Roleclaims makes sense when a claim attached to a role and not a user. Also, you may use them all together, depending on the need.
    – galdin
    May 29, 2018 at 1:29
  • Then I am a bit confused, You made 2 roles, and the Branch is a user claim?
    – chobo2
    May 29, 2018 at 22:03
  • 1
    Right, branch makes sense but why did you choose to make roles,instead of just making a whole bunch of claims? Like in your model you will need to add TheRoleTheCanEditUser, TheRoleThatCanDeleteUsers. Or sacrifice that flexibility where if everything was a claim you would not have that problem.
    – chobo2
    May 30, 2018 at 16:20
  • 2
    This youtube tutorial shows using claims with roles side by side: youtube.com/watch?v=LJQBBvJ6tL0
    – Eli
    Feb 8, 2021 at 12:37

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.