Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Endpoint has authorization metadata but UseAuthorization is missing

Errorcontains authorization metadata, but a middleware was not found that supports authorization.

Short answer

An endpoint carries [Authorize] or RequireAuthorization, but the authorization middleware never ran for that request. Add app.UseAuthorization() to the pipeline, after UseRouting and UseAuthentication and before the endpoints are mapped. Wrong order is the other common cause.

  • Applies to ASP.NET Core 8, ASP.NET Core 9, .NET 9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open Program.cs and list the middleware calls in order. UseAuthorization must appear, and it must come after UseRouting and UseAuthentication.

    grep -n "app.Use\|app.Map" Program.cs
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on the first request to any endpoint with [Authorize] or RequireAuthorizationUseAuthorization is not calledFix
    2. 2UseAuthorization is present, but it comes before UseRouting or after the endpoint is already mappedUseAuthorization sits in the wrong placeFix

What does the error look like?

  • contains authorization metadata, but a middleware was not found that supports authorization.

Cause 1 of 2

UseAuthorization is not called

1Check

The check fails when the endpoint has authorization metadata and the authorization middleware has not marked the request as handled.

grep -n "UseAuthorization\|AddAuthorization" Program.cs

2Fix

Call UseAuthorization yourself. The framework adds it for you only when it detects the authorization services (AddAuthorization) and your code did not call it, so a missing registration or a custom setup leaves the gap.

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

3Verify

Run the app and request a protected endpoint. You get 401 or 403 or the data, not the exception.

curl -i https://localhost:5001/api/secure

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 2

UseAuthorization sits in the wrong place

1Check

Authorization needs the selected endpoint, so it must run after routing. Authentication comes before authorization, and UseCors comes before both when it is used.

grep -n "UseRouting\|UseCors\|UseAuthentication\|UseAuthorization\|Map" Program.cs

2Fix

Reorder the calls: routing, then CORS if you use it, then authentication, then authorization.

app.UseRouting();
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

3Verify

Restart the app and call the protected endpoint again.

curl -i https://localhost:5001/api/secure

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep one fixed order in Program.cs: exception handling, HTTPS, static files, routing, CORS, authentication, authorization, endpoints.
  • Add one integration test that calls a protected endpoint without a token and expects 401.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.