Skip to content

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:

.NET Core: Scaling with Redis and IDistributedCache

ASP.NET Core โ€œIOptions Snapshot Turns Slowโ€ โ€” The Startup Misuse

Security โ€” URL Length Can Kill Requests

Post Views: 4

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 *

October 2026
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Sep    

Most Viewed Posts

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

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps
  • Photoshop: Fix a Color Profile Mismatch That Makes Printed Output Look Nothing Like What You Saw On Screen
  • WordPress: Fix Search Results That Return Pages From a Theme You Deactivated Months Ago
  • Visual Studio: Fix a Test Project That Builds Fine Alone but Fails to Discover Any Tests After a NuGet Restore
  • ASP.NET Core: Fix a File Upload That Times Out on Slow Connections Only Because Kestrel’s Minimum Data Rate Feature Kicked In
  • JavaScript: Fix an Array Destructuring Default Value That Silently Never Applies Because null Was Passed Instead of Undefined

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (973)
  • How to make theater mode the default for Youtube (952)
  • How to add default value for Entity Framework migrations for DateTime and Bool (939)
  • Get the First and Last Word from a String or Sentence in SQL (847)
  • How to select distinct rows in a datatable in C# (837)

Recent Posts

  • CSS: Fix a prefers-color-scheme Media Query That Gets Silently Overridden by a Browser Extension’s Forced Dark Mode
  • Git: Fix a Merge Commit That Silently Drops a File Because Both Branches Deleted It Differently
  • HTML5: Fix a Native Lazy-Loading Image That Never Loads Because It Sits Inside a Hidden Tab Until the User Clicks It
  • The AI Prompt That Traces a Null Reference Exception Back to the Exact Line That First Produced the Null
  • The AI Prompt That Turns a Gym Membership Contract’s Fine Print Into a Plain-English List of Cancellation Steps

Social

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