What Happens Under the Hood of the [Authorize] Attribute in ASP.NET Core?

What happens under the hood of [Authorize] attribute in ASP.NET Core

As an ASP.NET Core developer, we use Authentication and Authorization in almost every application we build. Most of the time, securing an endpoint is as simple as dropping an [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.

Before we dive into the internal middleware pipeline, it helps to have your basic policies set up. If you haven't done this yet, check out my previous blogs on Claim Based Authorization in ASP.NET Core and building Dynamic Role & Permission Authorization."

Authorization process begins when the request came to Authorization middleware. All the endpoints present in the controller are mapped into a collection, which has the life of application lifetime. Here are the steps how it handles authorization of an endpoint when the request comes to the Authorization Middleware.

Authorization middleware flow

Endpoint Extraction & Cache Check

The middleware first extract the endpoint from HttpContext and check for a computed policy from policy cache. If we're executing the endpoint for the first time then the cache will not have that endpoint. 

Building the Policy

In the next step, it will try to create a policy based on the authorization metadata of that endpoint. The metadata is already added to the HttpContext when the request is initiated. Any attribute that is used for authorization is of type IAuthorizeData. [Authorize], [Authorize(Roles="Admin")], [Authorize(Policy="")] etc are implements IAuthorizeData.

Now, it has all the authorization metadata. Authorizationpolicy.CombineAsnc() will combine all the authrorization requirement into a policy.
E.g.: An endpoint has only the [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.

For [Authorize(Roles="")], it will create a policy with the RoleAuthorizationRequirement using AuthorizationPolicBuilder.RequireRole(string[] roles).

For [Authorize(Policy="")], it will add that policy requirements to current instance.

For CustomAuthorization Requirements, it will add all the requirement present with the IAuthorizationRequirementData for that endpoint to a policy.

The policy which gets created with the above condition in cached for future use with the endpoint. This is because, next time it won't have to evaluate the policy again. It will be same the lifetime of the app.

Authentication Check

[Authorize] has AuthenticationScheme property as well. It also adds these to policy. 

Next it will try to Authenticate with the schemes that are passed to the policy. If there is no schemes present then it will use the existing result and proceeds with the next steps.

IAllowAnonymous Check

After this step, it checks whether there exists any IAllowAnonymous type in the endpoint. If yes, it will pass the HttpContext to the next request pipeline.

If no, then it will attempt to authorize the policy which gets created in above steps. 

Creating the AuthorizationContext 

During the polic authorization process, first of all it will create an 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 HandleRequirementAsync method.

  • If the requirement is not present, it simply returns Task.CompletedTask and 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!

Comments