CURRICULUM PREVIEWRevision 1 · Open for reviewReview the full course
CONNECTING THE PIECES8 min read

Middleware as a request pipeline

Place cross-cutting behavior in a deliberate order.

Before you beginconfiguration

The idea

Middleware wraps the next component and can short-circuit a request. An exception handler placed outside authentication, authorization, and endpoints can translate their failures. Authentication establishes identity; authorization evaluates permission. Order changes observable behavior.

Follow the flow

Make it concrete

This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.

Focused example
C#
app.UseExceptionHandler("/error");
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/bookings", ListBookings)
   .RequireAuthorization("booking:read");

Decisions that matter

  • Middleware is appropriate for request-wide behavior, not hidden domain decisions.
  • A short-circuit must still return a documented status and response shape.
  • Test the order using anonymous, forbidden, successful, and throwing endpoints.

Your turn

Where would you add a correlation ID and how would you avoid trusting an unbounded incoming header?

Reveal a worked answer

Add correlation handling near the start so later logs can include it. Validate the supplied header length and allowed characters, or generate an ID. Return a consistent ID to the caller without putting user-controlled data into log message templates.

Keep exploring

One more piece connected.

Mark this lesson complete when you can explain the idea.