ZeroAlloc.Authorization.Generator
2.1.6
See the version list below for details.
dotnet add package ZeroAlloc.Authorization.Generator --version 2.1.6
NuGet\Install-Package ZeroAlloc.Authorization.Generator -Version 2.1.6
<PackageReference Include="ZeroAlloc.Authorization.Generator" Version="2.1.6" />
<PackageVersion Include="ZeroAlloc.Authorization.Generator" Version="2.1.6" />
<PackageReference Include="ZeroAlloc.Authorization.Generator" />
paket add ZeroAlloc.Authorization.Generator --version 2.1.6
#r "nuget: ZeroAlloc.Authorization.Generator, 2.1.6"
#:package ZeroAlloc.Authorization.Generator@2.1.6
#addin nuget:?package=ZeroAlloc.Authorization.Generator&version=2.1.6
#tool nuget:?package=ZeroAlloc.Authorization.Generator&version=2.1.6
ZeroAlloc.Authorization
Authorization primitives for .NET — ISecurityContext, IResourceSecurityContext<TResource>, IAuthorizationPolicy and its parameterized IAuthorizationPolicy<T1>…<T1, T2, T3> variants, [Policy], [RequirePolicy], [RequireAnyPolicy], AnonymousSecurityContext, and an AuthorizerFor<TRequest> dispatcher emitted by the bundled source generator.
Used by:
- AI.Sentinel — tool-call authorization for
IChatClient-based agents ZeroAlloc.Mediator.Authorization— request-handler authorization
Install
dotnet add package ZeroAlloc.Authorization
Targets net8.0, net9.0, net10.0. The package bundles a Roslyn source generator — no separate *.Generator install.
The contract
using ZeroAlloc.Results;
public interface ISecurityContext
{
string Id { get; }
IReadOnlySet<string> Roles { get; }
IReadOnlyDictionary<string, string> Claims { get; }
}
public interface IAuthorizationPolicy
{
ValueTask<UnitResult<AuthorizationFailure>> EvaluateAsync(
ISecurityContext ctx, CancellationToken ct = default);
}
Writing a policy
using ZeroAlloc.Authorization;
using ZeroAlloc.Results;
[Policy("AdminOnly")]
public sealed class AdminOnlyPolicy : IAuthorizationPolicy
{
public ValueTask<UnitResult<AuthorizationFailure>> EvaluateAsync(
ISecurityContext ctx, CancellationToken ct = default)
=> new(ctx.Roles.Contains("Admin")
? UnitResult<AuthorizationFailure>.Success()
: new AuthorizationFailure(AuthorizationFailure.DefaultDenyCode, "Admin role required"));
}
Bind it on a request type — [RequirePolicy] is class/struct-level only, and stacks:
[RequirePolicy("AdminOnly")]
public sealed record DeleteUserCommand(string UserId);
[RequirePolicy("ActiveTenant")]
[RequirePolicy("AdminOnly")]
public sealed record PurgeTenantCommand(string TenantId);
Host wiring
The bundled generator emits one AuthorizerFor<TRequest> subclass per [RequirePolicy]-decorated type plus an AddZeroAllocAuthorization() extension on IServiceCollection. Hosts call the extension at startup and resolve AuthorizerFor<T> per request:
using ZeroAlloc.Authorization.Generated;
builder.Services.AddZeroAllocAuthorization();
var authorizer = sp.GetService<AuthorizerFor<DeleteUserCommand>>();
if (authorizer is not null)
{
var result = await authorizer.EvaluateAsync(securityContext, ct);
if (result.IsFailure)
return Forbid(result.Error); // host maps Code / Reason to its outcome shape
}
Hosts can extend ISecurityContext
Hosts define their own subinterface for richer payloads. AI.Sentinel adds IToolCallSecurityContext : ISecurityContext with ToolName + Args. ZeroAlloc.Mediator.Authorization passes the request through IResourceSecurityContext<TRequest>. Inside the policy body, downcast:
public ValueTask<UnitResult<AuthorizationFailure>> EvaluateAsync(
ISecurityContext ctx, CancellationToken ct = default)
=> new(ctx is IToolCallSecurityContext tc && tc.ToolName == "delete_database"
? new AuthorizationFailure("tool.destructive", "destructive tool blocked")
: UnitResult<AuthorizationFailure>.Success());
I/O-bound policies
For checks that need to await something (tenant lookup, external claims validation), mark EvaluateAsync as async:
[Policy("ActiveTenant")]
public sealed class ActiveTenantPolicy(ITenantService tenants) : IAuthorizationPolicy
{
public async ValueTask<UnitResult<AuthorizationFailure>> EvaluateAsync(
ISecurityContext ctx, CancellationToken ct = default)
{
var active = await tenants.IsActiveAsync(ctx.Id, ct).ConfigureAwait(false);
return active
? UnitResult<AuthorizationFailure>.Success()
: new AuthorizationFailure("tenant.inactive", "tenant is suspended");
}
}
Generator diagnostics
The bundled generator emits eight compile-time diagnostics:
| ID | Fires when |
|---|---|
ZAUTH001 |
[RequirePolicy("X")] references a policy name with no matching [Policy("X")]. |
ZAUTH002 |
Two [Policy("X")] declarations share the same name. |
ZAUTH003 |
[Policy] class doesn't implement IAuthorizationPolicy. |
ZAUTH004 |
[Policy] class is abstract or static. |
ZAUTH005 |
[RequirePolicy] placed on a non-class/non-struct target. |
ZAUTH006 |
[RequireAnyPolicy] lists a single policy name; use [RequirePolicy] instead. |
ZAUTH007 |
[RequirePolicy("X", ...)] arguments don't match the shape of the policy's IAuthorizationPolicy<…> interface. |
ZAUTH008 |
A [Policy] class implements more than one IAuthorizationPolicy variant. |
Performance
BenchmarkDotNet (BDN ShortRun, .NET 10 release build, x64) — happy path on a simple role-check policy:
| Method | Mean | Allocated |
|---|---|---|
EvaluateAsync |
~99 ns | 0 B |
Source: benchmarks/ZeroAlloc.Authorization.Benchmarks/PolicyEvaluationBenchmarks.cs.
The zero-allocation contract is enforced by AllocationGate tests in tests/ZeroAlloc.Authorization.Tests/AllocationBudgetTests.cs, which assert a 0 B budget on every run for the allow and deny paths of EvaluateAsync, a parameterized [RequirePolicy] dispatch and a [RequireAnyPolicy] dispatch.
NativeAOT compatibility is checked separately:
<IsAotCompatible>true</IsAotCompatible>on the main library, so the IL2026/IL3050 analyzers fire on any reflection regression at build time.- The
samples/ZeroAlloc.Authorization.AotSmoke/console app, published withPublishAot=trueand run on each CI run.
License
MIT.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.0.1 | 245 | 11/22/2026 |
| 3.0.0 | 87 | 11/22/2026 |
| 2.3.1 | 642 | 11/17/2026 |
| 2.3.0 | 1,757 | 11/11/2026 |
| 2.2.1 | 1,896 | 10/29/2026 |
| 2.2.0 | 107 | 10/29/2026 |
| 2.1.9 | 347 | 10/28/2026 |
| 2.1.8 | 1,062 | 10/18/2026 |
| 2.1.7 | 325 | 10/16/2026 |
| 2.1.6 | 359 | 10/15/2026 |
| 2.1.5 | 1,848 | 9/30/2026 |
| 2.1.4 | 579 | 9/28/2026 |
| 2.1.3 | 1,131 | 9/20/2026 |
| 2.1.2 | 132 | 9/20/2026 |
| 2.1.1 | 121 | 9/20/2026 |
| 2.1.0 | 4,900 | 5/23/2026 |
| 2.0.2 | 166 | 5/22/2026 |
| 2.0.1 | 142 | 5/22/2026 |