ASP.NET Core v2.1: Authorization

View on Github

ASP.NET Core v2.1: Authorization

Gravatar for
By Damien Guard

This tutorial will show you how assign roles to your users, and use those claims to authorize or deny a user to access certain routes in the app. We recommend you to Log in to follow this quickstart with examples configured for your account.


I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: .NET Core SDK 2.1.300 | .NET Core 2.1.0 | ASP.NET Core 2.1.0 | Visual Studio 2017 15.7 or Visual Studio Code (Optional)

ASP.NET Core supports Role based Authorization which allows you to limit access to your application based on the user's role. This tutorial shows how to add role information to the user's ID token and then use it to limit access to your application.

To follow the tutorial, make sure you are familiar with Rules.

Login issues

Create a Rule to Assign Roles

Create a rule that assigns the following access roles to your user:

  • An admin role
  • A regular user role

To assign roles, go to the New rule page. In the Access Control section, create an empty rule.

Use the following code for your rule:

Update the code to check for your own email domain, or match your custom condition.

You can define more roles other than admin and user, or customize the whole rule, depending on your product requirements.

This quickstart guide uses for the claim namespace. We recommend that you use a namespace related to your own Auth0 tenant for your claims, for example,

For more information on custom claims, read User profile claims and scope.

Is the user prompted for login credentials?

Restrict Access Based on User Roles

Configure the OIDC authentication handler registration inside your ASP.NET application to inform it which claim in the ID Token contains the role information. Specify the RoleClaimType inside TokenValidationParameters. The value you specify must match the namespace you used in your rule.

You can use the Role based authorization mechanism to make sure that only the users with specific roles can access certain actions. Add the [Authorize(Roles = ?)] attribute to your controller action.

The sample code below restricts the action only to users who have the admin role:

Use Auth0 for FREE