An [Authorize] endpoint rejects a request with a bearer token
401 UnauthorizedShort answer
An [Authorize] endpoint without a policy name requires an authenticated user. A 401 means nobody was authenticated for that request. Check that the Authorization header carries a valid bearer token, that UseAuthentication runs before UseAuthorization, and that the bearer scheme is the default or named on the attribute.
Fix it fast
1Find out which cause you have
Send the request from a terminal with the token and read the status and the WWW-Authenticate header.
curl -i -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/values2Match what you see, then jump to its fix
- 1
The WWW-Authenticate header is present and no token appears in the request logThe request carries no valid Authorization headerFix - 2
Valid tokens are rejected only after you added UseCors or reordered the middlewareThe authentication middleware is missing from a hand-built pipelineFix - 3
The token is valid but the endpoint still treats the user as anonymousMore than one scheme exists and bearer is not the one usedFix - 4
Only an endpoint meant to be open returns 401A public endpoint falls under the default or fallback policyFix
- 1
What does the error look like?
401 UnauthorizedAuthorization Header sent: no Bearer token sent.
Cause 1 of 4
The request carries no valid Authorization header
1Check
The API returns 401 when the request has no valid Authorization: Bearer header. RFC 6750 defines the format as the word Bearer, a space, then the token.
curl -i https://localhost:5001/api/values2Fix
Send the header as Authorization: Bearer followed by one space and the token, with no extra quotes.
curl -i -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/values3Verify
The status changes from 401 to 200, or to 403 if the token is valid but lacks permission.
curl -s -o /dev/null -w "%{http_code}\n" -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/valuesConfirmed by learn.microsoft.com
Cause 2 of 4
The authentication middleware is missing from a hand-built pipeline
1Check
ASP.NET Core adds UseAuthentication and UseAuthorization for you only when your code did not call them. The middleware article says that when UseCors is called, the app must call UseAuthentication and UseAuthorization itself, in that order.
grep -n -E "UseCors|UseAuthentication|UseAuthorization" Program.cs2Fix
Call the three in the documented order.
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();3Verify
Repeat the request with a valid token. It now reaches the endpoint.
curl -i -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/valuesConfirmed by learn.microsoft.com
Cause 3 of 4
More than one scheme exists and bearer is not the one used
1Check
With several authentication schemes, either make the bearer scheme the default or name it on the attribute.
grep -n -E "AddAuthentication|AddJwtBearer|AddCookie" Program.cs2Fix
Pass JwtBearerDefaults.AuthenticationScheme to AddAuthentication, or select it on the endpoint.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer();
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]3Verify
The same token now authenticates and the endpoint returns data.
curl -i -H "Authorization: Bearer $TOKEN" https://localhost:5001/api/valuesConfirmed by learn.microsoft.com
Cause 4 of 4
A public endpoint falls under the default or fallback policy
1Check
[Authorize] without a policy name uses the default policy, which requires an authenticated user. A fallback policy applies the same rule to endpoints that carry no authorization metadata.
grep -rn -E "SetFallbackPolicy|FallbackPolicy|\[Authorize\]" --include=*.cs .2Fix
Mark intentionally public endpoints with [AllowAnonymous] or AllowAnonymous().
app.MapGet("/health", () => "ok").AllowAnonymous();3Verify
The endpoint answers without a token.
curl -i https://localhost:5001/healthConfirmed by learn.microsoft.com
How do I stop it coming back?
- Keep UseAuthentication directly before UseAuthorization, and write both calls explicitly.
- Test one protected route and one public route after every change to the pipeline.
- Log failures from JwtBearerEvents while debugging.
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.
Related fixes
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fixSecurity
An ASP.NET Core API answers 401 with Bearer error="invalid_token"
401 Unauthorized
invalid_token in the WWW-Authenticate header means the access token is expired, revoked, malformed or invalid for another reason.
3 causes, each with a fix