See More

A very common reason is a wrong site baseUrl configuration.\n

Current configured baseUrl = / (default value)\n

We suggest trying baseUrl = \n\n',document.body.prepend(n);var e=document.getElementById("__docusaurus-base-url-issue-banner-suggestion-container"),s=window.location.pathname,o="/"===s.substr(-1)?s:s+"/";e.textContent=o}document.addEventListener("DOMContentLoaded",function(){void 0===window.docusaurus&&insertBanner()})

Skip to main content

ZeroAlloc.Authorization

Authorization primitives for .NET. Five types — ISecurityContext, IAuthorizationPolicy, [Authorize], [AuthorizationPolicy], AnonymousSecurityContext — designed to be shared across hosts that need a unified policy contract.

Quick example

[AuthorizationPolicy("AdminOnly")]
public sealed class AdminOnlyPolicy : IAuthorizationPolicy
{
public bool IsAuthorized(ISecurityContext ctx) => ctx.Roles.Contains("Admin");
}

public sealed class UserService
{
[Authorize("AdminOnly")]
public Task DeleteUserAsync(string userId) { /* ... */ }
}

The contract package stops here. A host matches the [Authorize] policy name to the registered [AuthorizationPolicy] class and invokes IsAuthorized / IsAuthorizedAsync before dispatch.


What it isn't

This is a contract package, not a host. It ships interfaces and attributes — nothing else.

  • No dispatcher. Nothing calls IsAuthorized for you.
  • No DI registration. There is no AddZeroAllocAuthorization() extension.
  • No integration with ASP.NET Core, MVC, minimal APIs, or any specific framework.
  • No attribute scanner. Hosts walk the [AuthorizationPolicy]-attributed types themselves.

Hosts provide all of that:

  • AI.Sentinel — tool-call authorization for IChatClient-based agents.
  • ZeroAlloc.Mediator.Authorization (planned) — request-handler authorization.

If you need an integration that does not exist yet, write a host. The contract is small on purpose.


  • Getting started — install, write your first policy, attach [Authorize].
  • PoliciesIAuthorizationPolicy, sync vs async, structured Evaluate results.
  • Security contextISecurityContext, host-specific subinterfaces, the anonymous singleton.
  • Attributes[Authorize] and [AuthorizationPolicy] reference.
  • Host integration — how to wire a host to the contract.

Targets

net8.0, net9.0, net10.0. AOT-compatible — <IsAotCompatible>true</IsAotCompatible> is set on the main library and the samples/ZeroAlloc.Authorization.AotSmoke/ app is exercised on every CI run with PublishAot=true.

Note: in ASP.NET Core projects, using ZeroAlloc.Authorization; collides with using Microsoft.AspNetCore.Authorization; over the [Authorize] name. Use a using alias (using ZAuthorize = ZeroAlloc.Authorization;) or fully-qualify one side at the call site.