Minimal API Workshop
Module 3

Identity, Tokens & Authorization

Identity · JWT · policies · resource authz · hardening

1:00 – 2:15 PM

By the end you can

Authentication ≠ Authorization

Who are you? vs. May you do this?

Order is not negotiable

app.UseAuthentication();   // validate token -> ClaimsPrincipal
app.UseAuthorization();    // evaluate policies against it
app.MapProductEndpoints(); // endpoints come after both

// no principal        -> 401 Unauthorized
// principal, denied   -> 403 Forbidden

Two ways to get a token

MapIdentityApi<T>()

  • Register / login / refresh for free
  • Great default
  • Returns a bearer token

Issue your own JWT

  • Control the claims
  • Add role + customer_id
  • Authz without a DB hit

Validating the token

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(o => o.TokenValidationParameters = new()
    {
        ValidIssuer = jwt.Issuer, ValidAudience = jwt.Audience,
        IssuerSigningKey = new SymmetricSecurityKey(keyBytes),
        ClockSkew = TimeSpan.FromSeconds(30),
    });

Signing key lives in user secrets / env — never appsettings.json. HS256 needs ≥ 32 bytes.

Authorization as policy

builder.Services.AddAuthorizationBuilder()
    .AddPolicy("Staff", p => p.RequireRole("Staff"))
    .AddPolicy("OwnsOrder", p => p.AddRequirements(new OwnsOrderRequirement()));

var staff = products.MapGroup("/").RequireAuthorization("Staff");
staff.MapPost("/", CreateProduct);   // and PUT, DELETE

Policy on the group → new endpoints are protected by default. Fail safe.

Resource-based authorization

protected override Task HandleRequirementAsync(
    AuthorizationHandlerContext ctx, OwnsOrderRequirement req, Order order)
{
    if (ctx.User.IsInRole("Staff") ||
        ctx.User.FindFirstValue("customer_id") == order.CustomerId.ToString())
        ctx.Succeed(req);
    return Task.CompletedTask;
}

Return 404 (not 403) for another customer's order — a 403 confirms it exists.

Never trust the body for identity

Hardening quick hits

ConcernWhat you add
Brute force on loginRate limiter, fixed window on /api/auth/*
Abuse elsewhereGlobal sliding-window limiter
Cross-origin callsCORS with an explicit origin list
TransportHTTPS redirection + HSTS
Response headersnosniff, Referrer-Policy, remove Server
Lab

Lab 3 · Customers and Baristas

Ends at checkpoint-3

Module 3 — Security
1 / 1
Speaker notes

Keyboard

SpaceNext (reveals bullets, then advances)
Back
Home / EndFirst / last
gOverview grid
sSpeaker notes
tLight / dark
fFullscreen
pPrint / save PDF
?This help
EscClose help/overview, or exit to the site

Swipe left/right on touch screens.