Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

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

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Asp.Net Core

ASP.NET Core: Fix CORS Errors That Only Happen in Production, Never in Development

- 30.08.26 | 31.08.26 - ErcanOPAK

⚙️ CORS Works in Dev, Dies in Production

Your frontend calls your ASP.NET Core API fine on localhost, then throws a CORS error the moment both are deployed — even though you configured a CORS policy. The gap is almost always a mismatch between what you configured and what the browser actually sees as the origin.

🐞 The Problem

// This looks reasonable, and works on localhost...
builder.Services.AddCors(options =>
{
    options.AddPolicy("Default", policy =>
        policy.WithOrigins("http://localhost:3000")
              .AllowAnyHeader().AllowAnyMethod());
});
// ...but production's frontend is served from
// https://app.example.com, not localhost:3000 - so every
// production request gets silently blocked by the browser.

🔍 The Other Common Causes

1. Origin mismatch: the policy lists a dev URL only, or the
   production URL has a typo, wrong scheme (http vs https), or
   a trailing slash that doesn't match exactly.
2. Reverse proxy (nginx/IIS/Cloudflare) stripping or rewriting
   the Access-Control-* response headers before they reach the
   browser - CORS is enforced by the BROWSER based on response
   headers, so if a proxy drops them, it fails even though your
   app set them correctly.
3. AllowCredentials() combined with a wildcard origin - the CORS
   spec explicitly forbids "*" when credentials are allowed, and
   ASP.NET Core will throw or silently fail depending on version.

✅ The Fix: Environment-Specific Origins + Verify Headers

  • Read allowed origins from configuration per environment, never hardcode a single URL.
  • After deploying, check the ACTUAL response headers with curl or browser DevTools’ Network tab — don’t just trust your Program.cs code, verify what’s really arriving at the browser.

⚙️ Environment-Aware CORS Policy

var allowedOrigins = builder.Configuration
    .GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();

builder.Services.AddCors(options =>
{
    options.AddPolicy("Default", policy =>
        policy.WithOrigins(allowedOrigins)
              .AllowAnyHeader()
              .AllowAnyMethod());
});

// appsettings.Production.json:
// { "Cors": { "AllowedOrigins": ["https://app.example.com"] } }

🔎 Verify What’s Actually Being Sent

curl -i -H "Origin: https://app.example.com" \
     -X OPTIONS https://api.example.com/orders
# Check the response for Access-Control-Allow-Origin - if it's
# missing here, the proxy/server is the problem, not your CORS
# policy code.

A CORS error in the browser console always names the origin it blocked — read that message literally, because the fix is almost always exactly what it says: an origin your policy doesn’t list.

— Backend Architect

Related posts:

ASP.NET Core: Fix Swagger Docs That Omit a Valid Endpoint Because Two Actions Share the Same Route T...

ASP.NET Core: Fix Swagger Docs That Omit a Valid Endpoint Because Two Actions Share the Same Route T...

.NET Core Memory Grows Without Leaks

.NET Core Memory Grows Without Leaks

ASP.NET Core: Fix a FromBody Parameter Deserializing to All-Default Values Because Two Competing JSO...

ASP.NET Core: Fix a FromBody Parameter Deserializing to All-Default Values Because Two Competing JSO...

Views: 11

Post navigation

ASP.NET Core: Stop appsettings.json Secrets From Leaking Into Your Git History
HTML5: Fix Forms That Submit Twice When Users Double-Click Submit

Leave a Reply Cancel reply

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

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

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