[Authorize] attribute above a controller and moving on. But what actually happens when a web request hits that attribute? In this post, we'll peel back the layers and explore exactly how ASP.NET Core evaluates policies, claims, and requirements under the hood.Endpoint Extraction & Cache Check
Building the Policy
IAuthorizeData. [Authorize], [Authorize(Roles="Admin")], [Authorize(Policy="")] etc are implements IAuthorizeData.Authorizationpolicy.CombineAsnc() will combine all the authrorization requirement into a policy.[Authorize] attribute present, then the method will build a policy with AuthorizationPolicyBuilder.RequireAuthenticatedUser().Build(), which is considered as a Default policy when there is no policy or roles is present.[Authorize(Roles="")], it will create a policy with the RoleAuthorizationRequirement using AuthorizationPolicBuilder.RequireRole(string[] roles).[Authorize(Policy="")], it will add that policy requirements to current instance.IAuthorizationRequirementData for that endpoint to a policy.Authentication Check
[Authorize] has AuthenticationScheme property as well. It also adds these to policy. IAllowAnonymous Check
IAllowAnonymous type in the endpoint. If yes, it will pass the HttpContext to the next request pipeline.Creating the AuthorizationContext
AuthorizationContext. It used to determine whether a user is authorized to perform a task on the specific resource. Once the AuthorizationContext is created, it packs together the current user (ClaimsPrincipal), the resource being accessed, and all the requirements combined from your policy. Now comes the most critical part of the pipeline: evaluating the requirement handler associated to the policy.
Handler Resolution
To evaluate the requirements, the framework needs the handlers for the requirement. It queries the Dependency Injection (DI) container for IEnumerable<IAuthorizationHandler>. It resolves every single authorization handler registered in the application. If we have 15 different handlers for various policies across the app, the DI container pulls all 15 into memory for this request.
Global Execution
Next, it loops through that entire collection of handlers and only execute the requirements that are of type TRequirement. Here TRequirement is the type of requirements present in the authorization context. This execute in done b AuthorizationHandler<TRequirement> abstract base class HandleAsnc method.
When HandleAsync is invoked, this base class inspects the AuthorizationContext using .OfType<TRequirement>() to see if its specific requirement exists for this endpoint.
If the requirement is present in the context, it passes execution down to your custom
HandleRequirementAsyncmethod.If the requirement is not present, it simply returns
Task.CompletedTaskand silently skips it.
The Final Verdict
Inside the handler, when authorization logic passes, we call context.Succeed(requirement). ASP.NET Core evaluates these requirements using a strict AND condition—every requirement in the AuthorizationContext must be explicitly marked as succeeded.
By default, the framework does not short-circuit. It allows all handlers to run to completion so that auditing, telemetry, and logging side-effects always fire. Once the loop finishes, if every requirement has succeeded, the middleware hands the HTTP context to the next delegate in the pipeline to execute your controller action. If any requirement remains unfulfilled, or if a handler explicitly called context.Fail(), the pipeline halts immediately and returns a 401 Unauthorized or 403 Forbidden response.
Conclusion
Understanding this pipeline completely changes how we write secure applications. It removes the "magic" from the [Authorize] attribute and shows us exactly how policies, requirements, and handlers interact to protect the endpoints.
The biggest architectural takeaway here is performance. Because the framework resolves every registered handler from the DI container on every single authorization check, we should default to registering our custom handlers as Singleton. If we register 15 different handlers as Scoped or Transient, the framework will needlessly allocate memory for all 15 of them on every secure HTTP request, silently dragging down the API's throughput.
Building a complex, database-driven authorization system from scratch using these internal mechanics takes serious time. If you want to skip the trial-and-error and save hours of setup, you can go through the article Dynamic Role & permission Authorization in ASP.NET Core Web API. It includes the clean architecture setup, custom requirement data attributes, and highly optimized singleton handlers ready to drop into your next enterprise project.
Happy coding, and keep exploring the internals!
![What happens under the hood of [Authorize] attribute in ASP.NET Core What happens under the hood of [Authorize] attribute in ASP.NET Core](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjJImT4ns8KqTrEhdAt-jVWuRIJkfJ0AZrX9Gaxyr6FPZas0Cm1023jcKQiZ8fRJ-6xkb6VJHuz5IZt_fwR02QcqshlWNktEjj3r7sM6UAWJGUiycSePasQnaOITZ8-0EVN0trYm65cOyoyk-LyNhm_oEcit8VxJohb3eppJK-esYzztWXyIhZbY9cT9ZAJ/s1600-rw/Copy%20of%20Copy%20of%20Authorization%20in%20asp.net%20core%20identity.png)

Comments
Post a Comment