IfTrue IfTrue - 1 year ago 274
ASP.NET (C#) Question

Authorization by Role/Group in ASP.NET using Auth0

Thanks in advance for your help in this matter!

I was hoping someone could help me figure out how to authorize API access by Group assigned in the Auth0 Authorization extension.

I currently am using the [Authorize] attribute in the web api perfectly - it allows an api call if they have signed in successfully and blocks it if not.

However, if I try [Authorize(Roles = "myGroupName")] authorization fails. Same occurs if I add it to the users app_metadata manually in the Users dashboard on the Auth0 website instead of assigning through the extension.

My project is set up by following the Angular Quick Start and Asp.Net Quick Start. My webapiconfig where I validate the token server side is:

class WebApiConfig
public static void Register(HttpConfiguration configuration)
var clientID = WebConfigurationManager.AppSettings["auth0:ClientId"];
var clientSecret = WebConfigurationManager.AppSettings["auth0:ClientSecret"];

configuration.MessageHandlers.Add(new JsonWebTokenValidationHandler()
Audience = clientID,
SymmetricKey = clientSecret

configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });

Answer Source

The Auth0 Authorization extension currently supports authorization decisions through the concept of groups. You can create a group, assign users to that group and that configure an application to only be accessible to user within a specific group. All of this would be handled automatically and any user outside of the application expected groups would be denied complete access.

Your use case is a bit different, but valid nonetheless. You want the groups configured with the extension to be sent along the generated token so that the application itself makes authorization decisions based on those values.

In order for the groups configured within the extension to be sent along in the token, the first thing you need to do is request them. For this, you need to include the groups scope when performing the authentication requests.

Add the user's group membership to the outgoing token (which can be requested via the OpenID groups scope);

(emphasis is mine, source: Authorization Extension Docs, section Rule Behavior)

If you request a token using that scope and then decode it in jwt.io, you would get something similar to this (the actual groups would vary by user):

  "groups": [
  "iss": "https://[tenant].auth0.com/"

Now, for the validation of this information on the ASP .NET API side. Assuming the sample you're using is this one (ASP.NET Web API), the group information contained within the token would be mapped to the following claims:

  • Type: groups | Value: GROUP-1
  • Type: groups | Value: GROUP-2

This happens because of the logic that exists in the JsonWebToken class which handles arrays coming from the JWT payload by creating per-value claim that share the same type.

The final part is making sure the AuthorizeAttribute checks these claims of type groups instead of trying to lookup role claims. You should be able to accomplish this, by changing the RoleClaimType constant in the JsonWebToken class to have the value "groups" instead of "http://schemas.microsoft.com/ws/2008/06/identity/claims/role".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download