duendesoftware/duende-skills/duende-bff
Claude Code and agent skill by duendesoftware/duende-skills
- community
Checking that the SKILL.md file is still in the repository.
Duende BFF (Backend for Frontend) security framework for securing SPAs. Covers session management, API endpoint proxying, token management, anti-forgery protection, and integration with React/Angular/Blazor frontends.
Install
npx vskill@latest install duendesoftware/duende-skills/duende-bffSKILL.md
The same SKILL.md was published earlier as duendesoftware/duende-skills/duende-bff.
Duende BFF (Backend for Frontend)
When to Use This Skill
- Building or securing a SPA (React, Angular, Vue, Blazor WASM) that calls APIs requiring authentication
- Implementing the Backend-for-Frontend security pattern to keep access tokens out of the browser
- Configuring BFF session management, login/logout endpoints, and server-side sessions
- Proxying requests from a frontend to remote APIs while automatically attaching access tokens
- Adding CSRF/anti-forgery protection to APIs consumed by browser-based applications
- Integrating
Duende.BFFwithDuende.AccessTokenManagementfor automatic token refresh - Deploying a BFF behind a reverse proxy or configuring same-site cookie behavior
Core Principles
- Tokens Never Touch the Browser — The BFF holds all OAuth tokens server-side; the browser only ever sees an HTTP-only, Secure, SameSite cookie
- CSRF Protection Is Mandatory — Every BFF API endpoint must require the
X-CSRF: 1header; use.AsBffApiEndpoint()orMapRemoteBffApiEndpoint— never skip it without an explicit alternative - Cookie Configuration Determines Security Posture —
SameSite=Strictis preferred when the IDP is on the same site;Laxis acceptable when cross-site redirects are required after login - Server-Side Sessions for Production — The default in-memory cookie session is unsuitable for production; persist sessions with
Duende.BFF.EntityFramework - Token Management Is Automatic — BFF integrates with
Duende.AccessTokenManagement; never manually refresh tokens or pass raw access tokens to the frontend
Docs: https://docs.duendesoftware.com/bff/
Pattern 1: Setup and Registration (BFF v4)
BFF v4 uses a streamlined registration API that auto-configures OpenID Connect and cookie authentication with recommended defaults.
BFF v3 Registration
For projects still on v3, explicit scheme setup is required and MapBffManagementEndpoints() must be called manually:
Key Differences: V4 vs V3
| Feature | V4 | V3 | |
|---|---|---|---|
| Auth handler setup | ConfigureOpenIdConnect() / ConfigureCookies() | Manual AddCookie() / AddOpenIdConnect() | |
| Management endpoints | Auto-registered | MapBffManagementEndpoints() required | |
| Remote API token type | .WithAccessToken(RequiredTokenType.User) | .RequireAccessToken(TokenType.User) | |
| Session cleanup | .AddSessionCleanupBackgroundProcess() | EnableSessionCleanup option | |
| Token retriever | IAccessTokenRetriever (implement directly) | DefaultAccessTokenRetriever (inheritable) | |
| Multi-frontend | Built-in AddFrontend() API | Not supported | |
| Middleware control | AutomaticallyRegisterBffMiddleware option | Always automatic |
Pattern 2: Login and Logout Endpoints
In BFF v4, management endpoints (/bff/login, /bff/logout, /bff/user, /bff/backchannel-logout) are registered automatically by AddBff() with the implicit default frontend. In v3, they require an explicit call to MapBffManagementEndpoints().
Login — A browser navigation to /bff/login initiates an OIDC Authorization Code flow. After the IDP redirects back, the BFF sets an HTTP-only authentication cookie.
Logout — A browser navigation to /bff/logout signs the user out locally and initiates an OIDC end_session flow. It also revokes the refresh token automatically.
Pattern 3: CSRF / Anti-Forgery Protection
The BFF enforces a custom X-CSRF header on every protected endpoint. This triggers a CORS preflight for cross-origin requests, effectively preventing CSRF attacks. The header value is irrelevant — its presence is sufficient.
Local (Embedded) API Endpoints
Middleware Order
UseBff() must appear after UseRouting() but before UseAuthorization(). Incorrect order silently disables anti-forgery enforcement.
Skipping Anti-Forgery
For specific endpoints that cannot send the anti-forgery header (e.g., webhook receivers), use .SkipAntiforgery():
Skipping Response Handling (V4)
By default, BFF converts 401/403 responses from local API endpoints into JSON-friendly responses (no redirect). Use .SkipResponseHandling() to bypass this and trigger normal ASP.NET Core authentication redirects:
Conditional Anti-Forgery (V4)
In v4, DisableAntiForgeryCheck is a delegate that allows conditionally skipping anti-forgery per-request:
Pattern 4: Remote API Proxying
The BFF can act as a reverse proxy to APIs deployed on separate hosts. Requests carry only the session cookie; the BFF exchanges it for an access token before forwarding.
Install the YARP integration package:
Cookie Header Removal
The BFF removes the browser's Cookie request header before forwarding to a remote API (the API authenticates via the access token the BFF attaches; the BFF session cookie must never leave the BFF). MapRemoteBffApiEndpoint has always done this; YARP routes now do too (every route, with or without token metadata; both AddYarpConfig in 4.x and AddReverseProxy().AddBffExtensions()). Behavior change in patch releases 2.2.1, 2.3.1, 3.0.1, 3.1.1, 4.0.4, 4.1.3, 4.2.1, 4.3.1 (security fix).
The header is removed after YARP copies request headers and after the route's own transforms run, so a route transform that sets Cookie has no effect while removal is on. Transforms registered after AddBffExtensions (e.g. .AddTransforms(...) on the returned IReverseProxyBuilder) run later and can still set a specific Cookie (intentional); don't copy the browser's cookies back.
To opt out (not recommended):
then remove it per route with YARP: "Transforms": [ { "RequestHeaderRemove": "Cookie" } ] or .WithTransformRequestHeaderRemove("Cookie"). (In 2.x/3.x BffOptions is in Duende.Bff; in 4.x Duende.Bff.Configuration.)
Token Type Options
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/duende-bff/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/duende-bff)