Endpoint has authorization metadata but UseAuthorization is missing
contains 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.
Fix it fast
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.cs2Match what you see, then jump to its fix
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.cs2Fix
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/secureConfirmed by learn.microsoft.com
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.cs2Fix
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/secureConfirmed by learn.microsoft.com
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
.NET in Docker cannot load libldap for LDAP or Active Directory
Unable to load shared library 'libldap-2.5.so.0' or one of its dependencies
On Linux, .NET's LDAP code loads the OpenLDAP library at run time, and the .NET images do not ship it.
2 causes, each with a fixBack end
ReceiveAsync gives half of my WebSocket message in C#
result.EndOfMessage is false after the first ReceiveAsync
One ReceiveAsync call fills at most your buffer, so a message larger than the buffer arrives in several pieces.
2 causes, each with a fixBack end
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fix