⚙️ The CORS Policy That Was Never Actually Allowed to Exist
AllowAnyOrigin and AllowCredentials are individually reasonable CORS settings, but the CORS specification itself forbids combining them, because a server that both accepts cookies or auth headers and announces it will accept requests from literally any origin would hand any malicious site on the internet a working credentialed session – ASP.NET Core’s CORS middleware enforces that restriction not with a confusing runtime CORS failure in the browser, but with an immediate InvalidOperationException thrown the moment the app actually tries to apply a policy combining both, which usually means right at startup if the policy is exercised during the very first request.
🔎 The Problem
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowFrontend", policy =>
{
policy.AllowAnyOrigin() // accept requests from anywhere
.AllowAnyHeader()
.AllowCredentials(); // ...and also send credentials
});
});
// System.InvalidOperationException:
// The CORS protocol does not allow specifying a wildcard origin
// (AllowAnyOrigin) and credentials at the same time. Configure the
// CORS policy by listing individual origins if credentials needs to
// be supported.
//
// This throws as soon as the policy is evaluated against an actual
// cross-origin request - often effectively "at startup" in dev,
// since the frontend's very first API call triggers it immediately.
✅ Fix: List Explicit Origins Whenever Credentials Are Needed
- Replace AllowAnyOrigin() with WithOrigins(“https://app.example.com”, “https://staging.example.com”) listing every explicit origin that legitimately needs credentialed cross-origin access – this is the only configuration the CORS spec itself permits once AllowCredentials() is also in the policy, not an ASP.NET Core-specific limitation that can be worked around with a different method call.
- If the set of allowed origins is genuinely dynamic (a multi-tenant app where each customer has their own subdomain), use SetIsOriginAllowed(origin => /* validate against a known list */) instead of a wildcard – this still lets AllowCredentials() coexist with it, because the decision resolves to one specific, validated origin per request rather than literally any origin.
- If the API genuinely never needs to send cookies or use credentialed requests, dropping AllowCredentials() entirely and keeping AllowAnyOrigin() is a completely valid, simpler policy – the fix here isn’t inherently “always list explicit origins,” it’s making sure the credentials and wildcard-origin settings are never both true on the same policy.
🚨 Why This Is Dangerous
- The exception happens at the exact moment a real cross-origin request needs the policy evaluated, which in development often means it looks like a transient, confusing crash tied to whichever endpoint happened to be called first, rather than an obviously misconfigured CORS policy sitting in Program.cs the whole time.
- A policy copy-pasted from an older tutorial or a different project that genuinely needed just AllowAnyOrigin can pick up an added AllowCredentials() later – for a new feature needing cookie-based auth, say – without anyone registering that the two settings were never meant to be combined, since each one reads as reasonable in isolation.
AllowAnyOrigin and AllowCredentials each sound like a reasonable thing to want. Wanting both at once is the one combination CORS itself was specifically designed to refuse.
