I have an API using .NET Core 2.0. I already have JWT working with roles but I want to move to use it with permissions. I have a defined list of permissions and the idea is to create dynamic roles based on that permissions.
Because roles are dynamic I can't use role-based authentication, so I need to authenticate a user based on permissions.
Suppose you have a static list of permissions. For example:
- Create post
- Edit post
- View all posts
- View my posts
Now let's say I have roles:
public class Role
{
public int Id { get; set; }
public string Name { get; set; }
public List<RolePermission> Permissions { get; set; }
public bool Status { get; set; }
}
You can create a dynamic role and select a list of permissions from given static example list.
Now I want to create a Role called "Administrator" with this permissions:
- Create post
- Edit post
- View all posts
My idea is to have a JWT like this:
JWT Example:
{
"iat": 1416929061,
"jti": "802057ff9b5b4eb7fbb8856b6eb2cc5b",
"role": "Administrator",
"permissions": {
"posts": {
"actions": ["readall", "create", "edit"]
}
}
}
Now I just want to check for permissions in order to access a resource in the API.
The only solution is to create policies for every permission? How can I achieve this?
