Advertisement
Intermediate ASP.NET Core, Controllers vs Minimal APIs

How do you apply the same authorization, CORS, and rate limiting policies consistently across both controllers and minimal API endpoints in a hybrid application?

Short answer

Since both routing systems ultimately register into the same underlying ASP.NET Core endpoint routing system, policies configured centrally (via [Authorize]/RequireAuthorization, UseCors/RequireCors, and rate limiting policies) apply consistently whether attached via attributes…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Since both routing systems ultimately register into the same underlying ASP.NET Core endpoint routing system, policies configured centrally (via [Authorize]/RequireAuthorization, UseCors/RequireCors, and rate limiting policies) apply consistently whether attached via attributes (controllers) or fluent extension methods (minimal APIs) -- the underlying policy definitions (in AddAuthorization, AddCors, AddRateLimiter) are shared and referenced by name from either style.
builder.Services.AddAuthorization(options => options.AddPolicy("AdminOnly", p => p.RequireRole("Admin")));

// Controller usage
[Authorize(Policy = "AdminOnly")]
public class AdminController : ControllerBase { }

// Minimal API usage of the SAME policy
app.MapDelete("/admin/users/{id}", DeleteUser).RequireAuthorization("AdminOnly");
Real-world example
A hybrid application defines its 'AdminOnly' authorization policy once, applying it consistently to both a legacy AdminController (via attribute) and new minimal API admin endpoints (via RequireAuthorization), avoiding policy definition duplication across the two styles.
Likely follow-ups
  • Are there any policies that behave subtly differently between the two invocation styles?
  • How would you audit a hybrid codebase to ensure policy consistency across both styles?
Related topics