Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/274188a/auth0-roles-used-for-authorization-in-blazor

Aspire Blazor WebApp using Auth0 roles for Authorization
https://github.com/274188a/auth0-roles-used-for-authorization-in-blazor

aspire auth0 authentication authorization blazor dotnet9

Last synced: 1 day ago
JSON representation

Aspire Blazor WebApp using Auth0 roles for Authorization

Awesome Lists containing this project

README

        

# Blazor App with Aspire and Auth0 Integration

The app uses Auth0 to define roles, which are added to the identity token using a **post-login event trigger** on the Auth0 cloud side.
The Blazor app then maps the Auth0 roles to standard roles for use within the application using the ClaimsTransformation feature in the OpenID Connect middleware.

## Key Features

- **Roles Defined in Auth0**: Roles are set up in Auth0 and injected into the identity token via a post-login trigger.
- **Mapping Auth0 Roles**: The C# code translates Auth0 role claims into standard roles for use within the application.

### Example of Role Claim Mapping Using Trigger Post-Login Trigger

```javascript
exports.onExecutePostLogin = async (event, api) => {
const roleClaim = 'https://blazorserverapp.local';
if (event.authorization) {
api.idToken.setCustomClaim(${roleClaim}/roles, event.authorization.roles);
}
};
```

### Code To Map Auth0 Roles to Standard Roles

The application contains C# logic to map the Auth0 roles:

```csharp
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication;

namespace BlazorApp1.Services;
public class CustomClaimsTransformation : IClaimsTransformation
{
private readonly IConfiguration _configuration;

public CustomClaimsTransformation(IConfiguration configuration) => _configuration = configuration;

public Task TransformAsync(ClaimsPrincipal principal)
{
if (principal.Identity is ClaimsIdentity identity)
{
string? audience = _configuration["Auth0:Audience"];
var roleClaims = identity.FindAll($"{audience}/roles").ToList();

foreach (var roleClaim in roleClaims)
{
identity.AddClaim(new Claim(ClaimTypes.Role, roleClaim.Value));
}
}

return Task.FromResult(principal);
}
}
```