duendesoftware/duende-skills/identityserver-api-protection
Claude Code and agent skill by duendesoftware/duende-skills
- community
Checking that the SKILL.md file is still in the repository.
Protecting APIs with Duende IdentityServer: JWT bearer authentication, reference token introspection, scope-based authorization, DPoP/mTLS proof-of-possession validation, local API authentication, and multi-audience scenarios.
Install
npx vskill@latest install duendesoftware/duende-skills/identityserver-api-protectionSKILL.md
The same SKILL.md was published earlier as duendesoftware/duende-skills/identityserver-api-protection.
Protecting APIs with IdentityServer
When to Use This Skill
- Configuring JWT bearer authentication in an ASP.NET Core API to validate tokens from IdentityServer
- Setting up reference token introspection with
AddOAuth2Introspection - Handling both JWT and reference tokens in the same API using
ForwardReferenceToken - Implementing scope-based authorization policies
- Validating Proof-of-Possession tokens (DPoP and mTLS
cnfclaim) - Protecting APIs hosted in the same application as IdentityServer (local API authentication)
- Securing multi-audience API deployments
Docs: https://docs.duendesoftware.com/identityserver/apis/
Core Concepts
APIs are the resources that IdentityServer protects. Clients obtain access tokens from IdentityServer, then present those tokens to APIs. The API must validate the token and enforce authorization based on the token's claims (scopes, audience, subject, etc.).
Token Formats at the API
| Format | Validation Method | Revocable | Network Dependency | |
|---|---|---|---|---|
JWT (at+jwt) | Signature verification using issuer's JWKS | No (expires naturally) | None at validation time | |
| Reference | Introspection endpoint call | Yes (immediate) | Requires IdentityServer availability |
JWT Bearer Authentication
Basic Setup
Install the standard Microsoft JWT bearer package:
Configure the authentication handler:
Critical: JWT Type Validation
Always set ValidTypes to ["at+jwt"] to protect against JWT confusion attacks. Without this, an attacker could present an identity token (which is also a JWT signed by the same issuer) to an API:
IdentityServer sets the typ header to at+jwt on all access token JWTs (per RFC 9068). This is controlled by IdentityServerOptions.AccessTokenJwtType.
Audience Validation
The Audience property on JwtBearerOptions validates the aud claim in the access token. The audience value comes from the ApiResource name in IdentityServer:
If Audience is not set, audience validation is skipped (not recommended for production).
Multi-Audience APIs
When an API belongs to multiple logical resources, configure multiple valid audiences:
Reference Token Introspection
For APIs that receive reference tokens (opaque strings rather than JWTs), use the OAuth 2.0 introspection package:
Or use the introspection handler directly:
The ClientId and ClientSecret correspond to the ApiResource name and secret configured in IdentityServer:
Common Pitfall: Missing ApiSecrets
Handling Both JWT and Reference Tokens
Use ForwardReferenceToken from the Duende.AspNetCore.Authentication.JwtBearer package to support both token formats in a single API. This selector inspects the token: if it contains a dot (.) it is treated as a JWT; otherwise it is forwarded to the introspection handler.
How ForwardReferenceToken Works
The selector checks whether the incoming Bearer token string contains a dot (.):
- Contains a dot → treated as a JWT, validated by
AddJwtBearer - No dot → treated as a reference token, forwarded to
AddOAuth2Introspection
This is a simple heuristic: JWTs always contain dots (header.payload.signature), while reference tokens are opaque identifiers.
Scope-Based Authorization
Scope Claim Format
IdentityServer can emit scopes in two formats, controlled by EmitScopesAsSpaceDelimitedStringInJwt:
| Setting | Claim Format | Example | |
|---|---|---|---|
false (default) | JSON array | "scope": ["api1.read", "api1.write"] | |
true | Space-delimited string | "scope": "api1.read api1.write" |
Normalizing Scope Claims
When scopes are emitted as a space-delimited string, the scope claim appears as a single string value. To normalize it back to individual claims for easier policy checks, implement a custom IClaimsTransformation:
This transformation converts a space-delimited scope claim into individual scope claims, so authorization policies work consistently regardless of the format.
Defining Authorization Policies
Apply policies to endpoints:
Or with controllers:
Proof-of-Possession (PoP) Token Validation
Proof-of-Possession binds an access token to a specific client's cryptographic key, preventing token theft/replay. IdentityServer supports two PoP mechanisms: Mutual TLS (mTLS) and DPoP.
mTLS Confirmation (cnf Claim)
When mTLS is used, the access token contains a cnf claim with the SHA-256 thumbprint of the client certificate:
To validate at the API, confirm the cnf thumbprint matches the client certificate presented on the TLS connection:
DPoP Validation
DPoP (Demonstration of Proof-of-Possession) uses a separate proof JWT in the DPoP HTTP header. Use the Duende.AspNetCore.Authentication.JwtBearer package which provides built-in DPoP validation:
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/identityserver-api-protection/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/duendesoftware/duende-skills/identityserver-api-protection)