Role based authorization using Keycloak and .NET core

Ryan.Bartsch

Having a few minor issues with role based authorization with dotnet core 2.2.3 and Keycloak 4.5.0.

In Keycloak, I've defined a role of 'tester' and a client role 'developer' with appropriate role mappings for an 'admin' user. After authenticating to Keycloak; if I look at the JWT in jwt.io, I can see the following:

"realm_access": {
        "roles": [
            "tester"
        ]
    },
    "resource_access": {
        "template": {
            "roles": [
                "developer"
            ]
    },
    ...
},

In .NET core, I've tried a bunch of things such as adding [Authorize(Roles = "tester")] or [Authorize(Roles = "developer")] to my controller method as well as using a policy based authorization where I check context.User.IsInRole("tester") inside my AuthorizationHandler<TRequirement> implementation.

If I set some breakpoints in the auth handler. When it gets hit, I can see the 'tester' and 'developer' roles listed as items under the context.user.Claims IEnumerable as follows.

{realm_access: {"roles":["tester"]}}
{resource_access: {"template":{"roles":["developer"]}}}

So I should be able to successfully do the authorization in the auth handler by verifying the values for realm_access and resource_access in the context.user.Claims collection, but this would require me to deserialize the claim values, which just seem to be JSON strings.

I'm thinking there has to be better way, or I'm not doing something quite right.

denizkanmaz

"AspNetCore.Authorization" expects roles in a claim (field) named "roles". And this claim must be an array of string (multivalued). You need to make some configuration on Keycloak side.

The 1st alternative:

You can change the existing role path.

Go to your Keycloak Admin Console > Client Scopes > roles > Mappers > client roles

  • Change "Token Claim Name" as "roles"
  • Multivalued: True
  • Add to access token: True

The 2nd alternative:

If you don't want to touch the existing path, you can create a new Mapper to show the same roles at the root as well.

Go to your Keycloak Admin Console > Client Scopes > roles > Mappers > create

  • Name: "root client roles" (or whatever you want)
  • Mapper Type: "User Client Role"
  • Multivalued: True
  • Token Claim Name: "roles"
  • Add to access token: True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Role authorization in .net core 2.0 using Identity

.NET Core 3.1 role based authorization fails, getting 403 exception

Asp.NET Core MVC Role based Authorization

Role based authorization in ASP.NET Core 3.1 with Identity and ExternalLogin

Role Based Authorization in ASP.NET Core 6

Implementing role-based authorization using .NET MVC 5

Using JWT with role based authorization

ASP.NET Core Custom Role Based Authorization (Custom User.IsInRole)?

IdentityServer4 and ASP.NET Core5.0 Identity - Role based Authorization

IdentityServer4 Role Based Authorization for Web API with ASP.NET Core Identity

asp.net core 2.0 app crashes when role based authorization faild

ASP.Net Core 3.0 Windows Authentication with Custom Role Based Authorization

asp.net core 2.0 windows role based authorization always returns 403

ASP.NET Core 2.1 Identity: Role-based authorization -> Access Denied

IdentityServer4 Role Based Authorization for ASP.NET Core Identity = Access Denied

ASP.NET Core 3.1 Web API Role based authorization not working

Asp.net core mvc role-based authorization with Azure AD

ASP.NET Core 3.1 MVC Access Denied role based authorization - Conflict with custom UserClaimsPrincipalFactory

role based authorization using spring security

Disable Input Fields in MVC View on Role Based Authorization using ASP.net Identity

Using Authorization filters to Implement permission based authorization in asp.net core

Role based jwt authorization

Role Based Authorization in Reactjs

graphql role based authorization

Claim based authorization in ASP.NET Core

Asp.NET Core Claim based Authorization

Claims based authorization in ASP.NET Core

Authorization Role/Policy Attributes Not Working In .Net Core 3

Windows Authentication Asp.net core 2 database role authorization