HTTP/1.1 403 Forbidden from a RequireRole("admin") endpoint while
the JWT clearly carries "roles": "admin": in this test the cause was
RoleClaimType = "roles" set in TokenValidationParameters while
the JWT handler's inbound claim mapping had already renamed roles to the long
http://schemas.microsoft.com/ws/2008/06/identity/claims/role type. The app
looked for a claim type that no longer existed.
Make the two settings agree. Either remove RoleClaimType and let the mapping
do its work, or keep RoleClaimType = "roles" and switch the mapping off with
options.MapInboundClaims = false. Both returned 200 in the test.
The error
There is no exception, and at the template's log levels nothing in the server console:
the endpoint answers 403 with an empty body. (With the authorization category raised to
Information the console names the failed role requirement, as shown in
the
RolesAuthorizationRequirement post.) A test endpoint /me that lists the claims the app sees shows why:
PS> curl.exe -s -i -H "Authorization: Bearer $token" http://127.0.0.1:15630/me
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
...
["aud = fixlab","iss = fixlab","exp = 1794620828","http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier = maria.garcia","http://schemas.microsoft.com/ws/2008/06/identity/claims/role = admin","iat = 1794619028","nbf = 1794619028","IsInRole(\"admin\") = False"]
PS> curl.exe -s -i -H "Authorization: Bearer $token" http://127.0.0.1:15630/reports
HTTP/1.1 403 Forbidden
Content-Length: 0
...
Why it happens
The token was made by the app's own /token endpoint with a made-up test key
and the claims sub = maria.garcia and roles = admin. The
validation settings were:
...
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "fixlab",
ValidAudience = "fixlab",
IssuerSigningKey = key,
RoleClaimType = "roles"
};
...
The claim list above is the evidence. sub arrived as the
nameidentifier URI and roles as the role URI, so
MapInboundClaims was on (it was not set, so this is the default of
JwtBearer 10.0.0). IsInRole("admin") checks claims of the type named by
RoleClaimType, which was "roles", and no claim of that type was
left. The role was there under another name, and the policy failed.
With the RoleClaimType line removed and nothing else changed, a token with
the same claims passed: /me showed the same role URI claim with
IsInRole("admin") = True, and /reports answered 200.
The fix
...
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.MapInboundClaims = false;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = "fixlab",
ValidAudience = "fixlab",
IssuerSigningKey = key,
RoleClaimType = "roles"
};
});
...
Now the claims keep their names from the token and the role check finds them:
PS> curl.exe -s -i -H "Authorization: Bearer $token" http://127.0.0.1:15630/me
...
["aud = fixlab","iss = fixlab","exp = 1794620831","sub = maria.garcia","roles = admin","iat = 1794619031","nbf = 1794619031","IsInRole(\"admin\") = True"]
PS> curl.exe -s -i -H "Authorization: Bearer $token" http://127.0.0.1:15630/reports
HTTP/1.1 200 OK
...
Turning the mapping off changed two names in the claim lists, not only the role:
sub stayed sub instead of becoming the
nameidentifier URI, and roles stayed roles;
aud, iss, exp, iat and nbf
were the same in both runs. If other code reads the long names, deleting the
RoleClaimType line is the smaller change. Here the /me endpoint
showed the cause of the 403 in one request: the role claim under the long URI and
IsInRole("admin") = False.
How it was reproduced
A minimal API made with dotnet new web -f net10.0 on SDK 11.0.100, with
Microsoft.AspNetCore.Authentication.JwtBearer 10.0.0, running on
Microsoft.AspNetCore.App 10.0.15. The token came from the app's own
/token endpoint (JsonWebTokenHandler, HS256, a test-only key
written in the code). The app was built and started by a small script, start.ps1, which runs dotnet build -nologo -v q in the project folder and then bin\Debug\net10.0\FixLab.exe --urls http://127.0.0.1:15630. The token was read into PowerShell with
$token = curl.exe -s http://127.0.0.1:15630/token, and sent with
curl.exe. Three runs used the same token shape: RoleClaimType = "roles"
alone (403), with MapInboundClaims = false (200), and with no
RoleClaimType (200).
Frequently asked
- Why does ASP.NET Core return 403 when my JWT has the admin role?
- The app looks for roles under the claim type set in RoleClaimType. In the test, RoleClaimType was "roles" but the JWT handler had renamed the roles claim to the long role URI, so IsInRole returned false and the policy answered 403.
- What does MapInboundClaims do in JwtBearer?
- With it on, which was the default in the test, short JWT claim names such as sub and roles arrived in the app as long URI claim types. With MapInboundClaims = false they kept the names from the token.
- How do I see which claims ASP.NET Core read from my token?
- Add a small authenticated endpoint that returns user.Claims as type and value pairs, plus user.IsInRole for the role you expect, and call it with the same token. It shows the claim types the role check will look at.
More decoded errors in the Fixes category.