Cocoar.Auth.Client.AspNetCore
0.2.1
See the version list below for details.
dotnet add package Cocoar.Auth.Client.AspNetCore --version 0.2.1
NuGet\Install-Package Cocoar.Auth.Client.AspNetCore -Version 0.2.1
<PackageReference Include="Cocoar.Auth.Client.AspNetCore" Version="0.2.1" />
<PackageVersion Include="Cocoar.Auth.Client.AspNetCore" Version="0.2.1" />
<PackageReference Include="Cocoar.Auth.Client.AspNetCore" />
paket add Cocoar.Auth.Client.AspNetCore --version 0.2.1
#r "nuget: Cocoar.Auth.Client.AspNetCore, 0.2.1"
#:package Cocoar.Auth.Client.AspNetCore@0.2.1
#addin nuget:?package=Cocoar.Auth.Client.AspNetCore&version=0.2.1
#tool nuget:?package=Cocoar.Auth.Client.AspNetCore&version=0.2.1
Cocoar.Auth.Client.AspNetCore
ASP.NET Core integration for resource servers that authenticate against a Cocoar.Auth identity provider.
The lib does two things on top of vanilla AddJwtBearer:
- Fetches
{Authority}/connect/userinfoon token validation and merges theresource_access[<audience>]block onto the principal. - Flattens that block into native
ClaimTypes.Role/"permission"/"group"claims so[Authorize(Roles = "...")]and an.RequiresCocoarPermission("...")endpoint filter work natively.
Bypass tiers (realm:admin, <resource>:admin) are pre-expanded
IdP-side before emission, so the client lib does pure
exact-match — no evaluator logic, no HTTP client, no caching.
Install
dotnet add package Cocoar.Auth.Client.AspNetCore
Quickstart
using Cocoar.Auth.Client.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.Authority = "https://auth.cocoar.dev";
options.Audience = "event-tree-api"; // matches an OAuthApi in the IdP
});
builder.Services.AddCocoarAuthClient(o =>
{
o.Authority = "https://auth.cocoar.dev";
o.Audience = "event-tree-api"; // same value as above
});
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
// Role-gated — uses the standard [Authorize] attribute since
// roles are projected to ClaimTypes.Role.
app.MapGet("/admin/ping", () => "pong")
.RequireAuthorization(p => p.RequireRole("Editor"));
// Permission-gated — bare 2-segment string. The IdP has already
// expanded realm:admin / <resource>:admin to catalog entries, so
// this is a pure contains-check.
app.MapPost("/calendars/{id}", (string id) => Results.Ok())
.RequiresCocoarPermission("calendar:write");
app.Run();
How the claims land on the principal
The IdP emits permissions per audience in Keycloak shape:
"resource_access": {
"event-tree-api": {
"roles": ["Editor", "Viewer"],
"permissions": ["calendar:read", "calendar:write"],
"group": ["Calendar Team"]
}
}
CocoarAuthClaimsTransformation projects that into flat claims:
| Source field | Flat claim type |
|---|---|
roles |
ClaimTypes.Role |
permissions |
"permission" |
group |
"group" |
Read them with standard claims APIs:
var perms = ctx.User.FindAll("permission").Select(c => c.Value);
Configuration reference
| Option | Description |
|---|---|
Authority |
IdP base URL. Used to fetch {Authority}/connect/userinfo. Same value as JwtBearerOptions.Authority. |
Audience |
The audience this resource server identifies as — same value as JwtBearerOptions.Audience. Looked up against resource_access[…]. |
JwtBearerScheme |
Scheme name to attach to. Defaults to "Bearer". |
License
Apache-2.0. See LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.