Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

An [Authorize] endpoint rejects a request with a bearer token

Error401 Unauthorized

Short 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.

  • Applies to ASP.NET Core 8, 9 and 10, Microsoft.AspNetCore.Authentication.JwtBearer
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/values
  2. 2Match what you see, then jump to its fix

    1. 1The WWW-Authenticate header is present and no token appears in the request logThe request carries no valid Authorization headerFix
    2. 2Valid tokens are rejected only after you added UseCors or reordered the middlewareThe authentication middleware is missing from a hand-built pipelineFix
    3. 3The token is valid but the endpoint still treats the user as anonymousMore than one scheme exists and bearer is not the one usedFix
    4. 4Only an endpoint meant to be open returns 401A public endpoint falls under the default or fallback policyFix

What does the error look like?

  • 401 Unauthorized
  • Authorization 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/values

2Fix

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/values

3Verify

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/values

Confirmed by learn.microsoft.com

Did this fix it?

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.cs

2Fix

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/values

Confirmed by learn.microsoft.com

Did this fix it?

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.cs

2Fix

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/values

Confirmed by learn.microsoft.com

Did this fix it?

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/health

Confirmed by learn.microsoft.com

Did this fix it?

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

Next step

Want this handled for good?

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