Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

Asp.Net Core: Fix CORS Preflight Blocked by a Custom Header Name

- 30.11.26 - ErcanOPAK

⚙️ 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.

— Backend Architect

Related posts:

.NET Core: Master DI Lifecycles (Transient vs Scoped vs Singleton)

.NET Core: Master DI Lifecycles (Transient vs Scoped vs Singleton)

ASP.NET Core: Fix a Background Service That Silently Stops Running After One Unhandled Exception

ASP.NET Core: Fix a Background Service That Silently Stops Running After One Unhandled Exception

.NET Core: Use Health Checks for Monitoring

.NET Core: Use Health Checks for Monitoring

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

HTML5: Fix a Picture Element Serving the Wrong Art Direction Source
Asp.Net Core: Fix Razor Pages Page Filter Running Before Auth

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com