⚙️ Fix CORS Preflight Blocked by a Custom Header Name
A browser sends an OPTIONS preflight before any cross-origin request that includes a non-simple header, and ASP.NET Core’s CORS middleware only allows headers that were explicitly listed with WithHeaders(...) in the policy — a custom header added to the client’s fetch call (an API key, a trace ID) without adding that exact header name to the server’s CORS policy makes the preflight itself fail, which surfaces in the browser as a generic CORS error that has nothing to do with the actual endpoint’s logic.
🔎 The Problem
builder.Services.AddCors(options =>
{
options.AddPolicy("ApiPolicy", policy =>
policy.WithOrigins("https://app.example.com")
.WithMethods("GET", "POST")
.WithHeaders("Content-Type", "Authorization"));
// Missing: "X-Client-Trace-Id" sent by the client
});
// Browser console: Access to fetch at '...' from origin '...' has been
// blocked by CORS policy: Request header field x-client-trace-id is
// not allowed by Access-Control-Allow-Headers
✅ Fix: Add Every Non-Simple Header the Client Actually Sends
- List the custom header explicitly in
WithHeaders(...)alongside the standard ones, since CORS requires every non-simple request header to be named, not just the ones that feel significant. - During development, check the exact failing header name in the browser console error message rather than guessing — it names the specific header the preflight rejected.
- If the set of custom headers changes often, consider
AllowAnyHeader()for an internal API behind other access controls, rather than maintaining an exact header allow-list by hand on every change.
A CORS preflight failure on a custom header has nothing to do with your endpoint’s code — the request never got there.





