Why can't AllowCredentials() be combined with a wildcard origin (AllowAnyOrigin), and what does this mean for cookie-authenticated cross-origin APIs?
Short answer
The CORS specification explicitly forbids combining AllowCredentials (needed for cookie-based auth or client certificates across origins) with a wildcard origin, as a security measure -- if credentialed requests were allowed from any origin, it would defeat CORS's core purpose…
Advertisement
AModel answer
The CORS specification explicitly forbids combining AllowCredentials (needed for cookie-based auth or client certificates across origins) with a wildcard origin, as a security measure -- if credentialed requests were allowed from any origin, it would defeat CORS's core purpose of restricting which sites can make authenticated requests on a user's behalf, so you must specify exact allowed origins when credentials are involved.
// INVALID combination -- throws at runtime
// policy.AllowAnyOrigin().AllowCredentials();
// Correct: specific origins required with credentials
policy.WithOrigins("https://app.mycompany.com")
.AllowCredentials()
.AllowAnyHeader().AllowAnyMethod();Real-world example
A team's cookie-based authentication mysteriously stopped working cross-origin after someone changed a CORS policy from specific origins to AllowAnyOrigin(), not realizing this silently disables credentialed requests due to the spec-level incompatibility.
Likely follow-ups
- Why is combining wildcard origins with credentials considered a security risk?
- How do JWT bearer tokens sidestep this particular constraint compared to cookies?
Related topics