ASP.NET (OWIN) Authorization

Sample Project

Download this sample project configured with your Auth0 API Keys.

Many identity providers will supply access claims, like roles or groups, with the user. You can request these in your token by setting scope: openid roles or scope: openid groups. However, not every identity provider provides this type of information. Fortunately, Auth0 has an alternative to it, which is creating a rule for assigning different roles to different users.

This tutorial assumes that you've already read the rules tutorial and you know how to implement a basic rule in your app.

Create a Rule to assign roles

First, we will create a rule that assigns our users either an admin role, or a single user role. To do so, go to the new rule page and create an empty rule. Then, use the following code for your rule:

function (user, context, callback) {
  var addRolesToUser = function(user, cb) {
    if ('') > -1) {
      cb(null, ['admin']);
    } else {
      cb(null, ['user']);

  addRolesToUser(user, function(err, roles) {
    if (err) {
    } else {
      context.idToken[""] = roles;     
      callback(null, user, context);

Update the code to check for your own email domain, or match the condition according to your needs. Notice that you can also set more roles other than admin and user, or customize the whole rule as you please.

This quickstart uses for the claim namespace, but it is suggested that you use a namespace related to your own Auth0 tenant for your claims, e.g

Restrict an action based on a user's roles

As with the country property which was added in the previous step, you will also need to manually extract the roles from the user in the OnAuthenticated event and add the appropriate claims. For each role, you can add a role of the type ClaimTypes.Role.

This will ensure proper integration with the existing role-based authorization in ASP.NET MVC and allow you to restrict access to a controller by simply decorating your controller actions with the [Authorize(Roles = ?)] attribute

So change the existing middleware registration in the Startup class to extract the roles and add the claims:

var options = new Auth0AuthenticationOptions()
    Domain = auth0Domain,
    ClientId = auth0ClientId,
    ClientSecret = auth0ClientSecret,

    Provider = new Auth0AuthenticationProvider
        OnAuthenticated = context =>
            // Get the user's country
            JToken countryObject = context.User[""];
            if (countryObject != null)
                string country = countryObject.ToObject<string>();

                context.Identity.AddClaim(new Claim("country", country, ClaimValueTypes.String, context.Connection));

            // Get the user's roles
            var rolesObject = context.User[""];
            if (rolesObject != null)
                string[] roles = rolesObject.ToObject<string[]>();
                foreach (var role in roles)
                    context.Identity.AddClaim(new Claim(ClaimTypes.Role, role, ClaimValueTypes.String, context.Connection));

            return Task.FromResult(0);

Now you can add a new action to your controller and restrict it by decorating your controller actions with the [Authorize(Roles = ?)] attribute.

The sample code below will restrict the particular action only to the user who have the "admin" role:

[Authorize(Roles = "admin")]
public ActionResult Admin()
    return View();
Previous Tutorial
6. Rules
Use Auth0 for FREECreate free Account