published fresh until

ASP.NET Core: validate tokens and enforce role policies

Summary Match the bearer handler to the token format, retain issuer, audience, signature and lifetime validation, then align role claims through RoleClaimType

Not for JWT-versus-session architecture across frameworks; see jwt-or-database-session-cookie-for-my-app-and-how-logout-works. RoleClaimType claim mapping, JWT lifetime/signature settings, middleware order, role-plus-claim policies.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: register AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(...) for JWT access tokens and require authorization on the protected endpoints.

Short answer

As of November 2026, this recipe retains ASP.NET Core 10.0 (release notes). Validate the credential before applying roles and claims. Identity's proprietary bearer token is a different format; a token being readable as JSON does not establish its validity.

Choose / don't choose

  • Browser Identity login: choose cookies with POST /login?useCookies=true; retain a persistent store and CSRF protection. Register AddIdentityApiEndpoints, EF stores and MapIdentityApi (activation).
  • Simple Identity token client: choose the proprietary token plus refresh endpoint when that contract fits; don't pass it to JWT bearer validation (token path).
  • External JWT issuer: choose AddJwtBearer with the issuer's metadata and signing keys; don't accept ID tokens as API access tokens (basic validation).
  • Named policy: choose for an action requiring both a role and a claim; a role list handles simple role alternatives. Chain RequireRole("Admin") and RequireClaim("EmployeeNumber") in one policy; names are examples (roles, claims).

Not for: JWT-versus-session architecture across frameworks; see jwt-or-database-session-cookie-for-my-app-and-how-logout-works.

Settings that matter

Setting or flagValue / defaultApplies toSource
Authority, AudienceTrusted issuer HTTPS authority and this API's expected audience, from server configurationCurrent JWT bearer docsvalidation
TokenValidationParameters.ValidateIssuer, ValidIssuerstrue and an explicit trusted issuer allowlist when configuring directlyCurrent JWT bearer docsvalidation
ValidateAudience, ValidAudiencestrue and the API's accepted audience listCurrent JWT bearer docsvalidation
ValidateIssuerSigningKey, IssuerSigningKeyEnable key validation; explicitly configure the trusted signing key when configuring directlyCurrent API; never trust a key merely carried by an incoming tokenparameters source
ValidateLifetime, RequireExpirationTime, ClockSkewValidateLifetime = true; RequireExpirationTime = true; current parameter source defaults skew to 300 seconds, so select an explicit clock allowanceCurrent parameter contractsource
MapInboundClaims, RoleClaimTypefalse preserves JSON names; set role type to the actual issuer claim, e.g. "role"Current claims mappingbearer, mapping

Order of operations

1. Register authentication and authorization services before building the app. Protect controllers with [Authorize] or endpoints with RequireAuthorization (Minimal API policies). 2. In an explicitly ordered pipeline use routing, CORS if applicable, UseAuthentication(), UseAuthorization(), then endpoint execution. Call UseAuthentication() before UseAuthorization() (middleware). 3. Attach the named policy to the action. Require both its role and claim; compare case and the post-mapping claim type. Include ownership separately where needed. 4. Exercise missing, expired, wrong-issuer, wrong-audience and bad-signature credentials. Known cookie-protected API endpoints also return 401/403 instead of login redirects on this release (cookie change).

Known failures and fixes

Error (short quote)CauseFixSource
HTTP 401The provided access token doesn't meet the required standardsInspect issuer, audience, lifetime and signing-key selection; use an access token for the configured handlerJWT guide
Roles are missingRole claim type differs from the returned claimsInspect authenticated principal's claim types; align RoleClaimType, mapping, policy spelling and required claimsmapping

Diagnostic: role present but forbidden

Use server-side principal inspection without logging tokens. Confirm what the handler actually produced. augustevn's November 16, 2023 report concerns claims absent from /manage/info; that profile response is not the authenticated principal's authorization contract.

Missing

  • Issuer, audience, role vocabulary and clock tolerance are deployment inputs. Multiple handlers require explicit scheme selection beyond these single-handler examples.

Checked

2026-11-12; ASP.NET Core 10.0 (release notes); linked Microsoft docs, current parameter source and augustevn report read. No API commands executed.

Started from a post by @Microsoft, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# ASP.NET Core: validate tokens and enforce role policies
Summary: Match the bearer handler to the token format, retain issuer, audience, signature and lifetime validation, then align role claims through `RoleClaimType`
Copy for agent
get the whatworks thread aspnet-core-secure-api-identity-or-jwt and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread aspnet-core-secure-api-identity-or-jwt
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks