Authentication and authorization in .NET
Establish identity, then check the resource.
The idea
A valid token identifies a principal, but it does not grant access to every booking. Validate the issuer, audience, signature, and lifetime using the platform authentication handler. Apply coarse endpoint policies and a tenant-scoped resource check. Derive tenant membership from trusted identity and data, not a free-form header.
Follow the flow
Make it concrete
This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.
builder.Services.AddAuthorization(options =>
options.AddPolicy("booking:read", policy =>
policy.RequireAuthenticatedUser()
.RequireClaim("permission", "booking:read")));
// The query still includes the caller's authorized tenant.
app.MapGet("/bookings/{id:guid}", GetBooking)
.RequireAuthorization("booking:read");Decisions that matter
- Prefer established identity middleware over a hand-built token validator.
- Cookie-authenticated state changes need CSRF protection.
- Recheck authorization for each object operation, including exports and background actions.
Your turn
A user with booking:read requests a booking ID from a different tenant. Why is the endpoint policy insufficient?
Reveal a worked answer
The claim authorizes the operation category, not ownership of the requested record. Scope the query to an authorized tenant and test that cross-tenant IDs reveal no booking data. Use a consistent missing/forbidden policy to limit enumeration.