Auth0 Token Forwarding to Backend APIs

Attaches Auth0 access tokens to outgoing ASP.NET Core HttpClient requests.

Updated Mar 18, 2022
One-click install
npx skills add https://github.com/mpaulosky/dotfiles --skill auth0-token-forwarding-to-backend-apis
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Auth0 Token Forwarding to Backend APIs
Source: https://github.com/mpaulosky/dotfiles/tree/main/.copilot/skills/auth0-token-forwarding
Command: npx skills add https://github.com/mpaulosky/dotfiles --skill auth0-token-forwarding-to-backend-apis

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

When a frontend authenticated with Auth0 OIDC makes HttpClient calls to protected backend APIs, the user's access_token is not automatically propagated which causes 401 Unauthorized errors; this skill shows how to attach the token from the user's authentication session to outgoing requests.

Core Features & Use Cases

  • Automatic token attachment: Use a DelegatingHandler to read the access_token from the current HttpContext and set Authorization: Bearer {token} on outgoing HttpClient requests.
  • Framework scope: Designed for Blazor Server and ASP.NET Core frontends calling separate backend APIs that validate JWT bearer tokens.
  • Operational guidance: Covers registration of IHttpContextAccessor, enabling SaveTokens in OIDC options, handler registration on HttpClient, testing patterns, and common pitfalls.

Quick Start

Create a TokenForwardingHandler, register IHttpContextAccessor, set SaveTokens = true in your Auth0 OIDC options, and add the handler to each HttpClient registration so outgoing requests include the Bearer token.

Frequently Asked Questions about Auth0 Token Forwarding to Backend APIs

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does my Blazor Server app get 401 Unauthorized when calling backend APIs with Auth0?

Your Blazor Server app gets 401 Unauthorized because the user's Auth0 access_token is not automatically attached to outgoing HttpClient requests. You must manually forward the token by setting the Authorization Bearer header on calls to protected backend APIs.

How do I forward Auth0 access tokens to HttpClient requests in ASP.NET Core?

To forward Auth0 access tokens in ASP.NET Core, create a DelegatingHandler that reads the access_token from the current HttpContext and sets the Authorization header. Register this handler on your HttpClient to automatically attach Bearer tokens to outgoing requests.

What is SaveTokens in OIDC and do I need it to forward Auth0 tokens?

SaveTokens is an OIDC configuration option that stores the access_token in the authentication session. You must set SaveTokens = true in your Auth0 OIDC options so the DelegatingHandler can read and forward the token to your backend APIs.

Can I use a DelegatingHandler to attach JWT bearer tokens in Blazor Server?

Yes, you can use a DelegatingHandler to attach JWT bearer tokens in Blazor Server. You need to register IHttpContextAccessor so the handler can access the current HttpContext, read the Auth0 access_token, and set the Authorization header on outgoing HttpClient requests.

What are common pitfalls when forwarding Auth0 tokens to backend APIs?

Common pitfalls when forwarding Auth0 tokens include forgetting to register IHttpContextAccessor, not enabling SaveTokens in OIDC options, and failing to add the DelegatingHandler to the HttpClient registration, which prevents the Bearer token from being attached to outgoing requests.