Advertisement
Intermediate .NET, CORS & Cross-Origin Resource Sharing

What does AllowCredentials() do in a CORS policy, and why can't it be combined with a wildcard origin?

Short answer

AllowCredentials() permits cross-origin requests to include credentials (cookies, HTTP authentication, client certificates), required for cookie-based authentication to work across origins.

Save .NET mock interview
Advertisement
AModel answer
AllowCredentials() permits cross-origin requests to include credentials (cookies, HTTP authentication, client certificates), required for cookie-based authentication to work across origins. The CORS specification explicitly disallows combining AllowCredentials with a wildcard (*) origin as a security measure -- if credentialed requests were allowed from literally 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 using credentials.
// This configuration throws an exception at runtime:
// policy.AllowAnyOrigin().AllowCredentials();  // INVALID combination

// 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 entirely 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 CORS/credentials constraint?
Related topics