Get permissions and roles with Auth0

With Auth0 we can define roles and permissions. Permissons have to be added from the Applications/API section, in the Permissions tab, as you can see in this picture:

For roles, you will have to go to the User Management / Roles menu option, where you will be abel to create as many roles as you want. Once you have created a user, you will be able to associate as many permissions as you want to the selected role, to do it, just open the user details and go to the Permissions tab:


 In order to get the permissions and roles defined in Auth0, you will have to create a new rule in your Auth0 dashboard. To do it, just go to the Auth Pipeline menu option, click in Rules and add a new rule with the next code:


To access to this data in our NET Core API, we'll do it with the principal object like you can see here:

var roles = principal.Claims.Where(x => x.Type == $"{this.AuthConfiguration.Namespace}/roles")?.Select(x => x.Value).ToList();
var permissions = principal.Claims.Where(x => x.Type == $"{this.AuthConfiguration.Namespace}/permissions")?.Select(x => x.Value).ToList();

Comentarios