duendesoftware/duende-skills/token-management
Claude Code and agent skill by duendesoftware/duende-skills
- community
Checking that the SKILL.md file is still in the repository.
Token management patterns using Duende.AccessTokenManagement. Covers client credential token caching, user token refresh, token storage, HttpClientFactory integration, DPoP support, and common configuration pitfalls. Also includes Blazor Server token management.
Install
npx vskill@latest install duendesoftware/duende-skills/token-managementSKILL.md
The same SKILL.md was published earlier as duendesoftware/duende-skills/token-management.
Token Management
When to Use This Skill
Use this skill when:
- Building a .NET worker service or daemon that calls APIs using the client credentials flow
- Building an ASP.NET Core web application that calls APIs on behalf of the currently logged-in user
- Integrating
Duende.AccessTokenManagementorDuende.AccessTokenManagement.OpenIdConnectwithIHttpClientFactory - Configuring token caching — in-memory, distributed (Redis), or hybrid — for machine-to-machine tokens
- Adding DPoP (Demonstrating Proof-of-Possession) key binding to access tokens
- Implementing API-to-API delegation where a downstream service calls further APIs with either user tokens or client credentials
- Revoking refresh tokens on user sign-out
Core Principles
- Prefer Automatic Over Manual — Use
IHttpClientFactory-integrated clients; they acquire, cache, refresh, and attach tokens transparently. CallGetAccessTokenAsyncmanually only when the factory pattern is insufficient. - Never Cache Tokens in Code — The library owns the cache. Do not store tokens in instance fields, static variables, or application-managed caches. Call the service on every request and let it serve from cache.
- `SaveTokens = true` Is Required for User Tokens — The OIDC handler must persist tokens into the authentication session. This is the most common misconfiguration.
- Refresh Tokens Must Be Revoked at Sign-Out — Call
e.HttpContext.RevokeRefreshTokenAsync()inOnSigningOutto revoke the refresh token at the authorization server, preventing reuse after logout. - v4 Uses `HybridCache`; v3 Uses `IDistributedCache` — The caching layer changed between major versions. v4's
HybridCacheis two-tier and automatic; v3 requires an explicitAddDistributedMemoryCache()or Redis registration. - Resiliency Is Included in `AddClientCredentialsHttpClient` — This registration adds a once-retry handler for
401 Unauthorizedresponses (handles token expiry and DPoP nonce challenges). When usingAddClientCredentialsTokenHandlerdirectly, add it explicitly.
Related Skills
aspnetcore-authentication— cookie and OIDC handler setup required for user token managementidentityserver-configuration— configuring the authorization server that issues tokensoauth-oidc-protocols— protocol fundamentals underlying client credentials and refresh token flowsduende-bff— BFF pattern integrates this library automatically for proxied API calls
Docs: https://docs.duendesoftware.com/accesstokenmanagement/
Pattern 1: Machine-to-Machine (Client Credentials) — Worker Services
Package
Registration
Available client options:
TokenEndpoint— URL of the OAuth token endpointClientId/ClientSecret— client credentialsClientCredentialStyle—AuthorizationHeader(default) orPostBodyScope— requested scope (optional; overridable per request)Resource— resource indicator per RFC 8707 (optional)HttpClientName— custom backchannel HTTP client name from the factoryDPoPJsonWebKey— JWK for DPoP-bound tokens (see Pattern 5)
Automatic via HttpClientFactory (Recommended)
Usage — no token code required at the call site:
Resiliency handler —AddClientCredentialsHttpClientautomatically adds a resiliency handler that retries once on401 Unauthorized. This covers token expiry and DPoP nonce challenges. When usingAddClientCredentialsTokenHandlerdirectly, add it explicitly:```csharpservices.AddHttpClient<CatalogClient>(...).AddDefaultAccessTokenResiliency().AddClientCredentialsTokenHandler("catalog.client");```
Manual Token Retrieval (Advanced)
In v3, the service wasIClientCredentialsTokenManagementServiceand the result was read via.Value. In v4 it isIClientCredentialsTokenManagerand the result isTokenResult<ClientCredentialsToken>— use.Succeeded/.GetToken().
Pattern 2: User Token Management — Web Applications
Package
Registration
`SaveTokens = true` is mandatory. Without it, the library cannot read or refresh the user's access token. This is the most common misconfiguration causingInvalidOperationExceptionat runtime.
Automatic via HttpClientFactory (Recommended)
Usage in a controller:
Manual Token Retrieval (Advanced)
HttpContext extension methods are also available:
gRPC Support
Use AddUserAccessTokenHandler and AddClientAccessTokenHandler when registering typed gRPC clients:
Pattern 3: Token Caching
v4 — HybridCache (Default)
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/token-management/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/token-management)