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

How do you configure CORS to allow requests only from multiple specific origins, such as both a production and staging frontend?

Short answer

WithOrigins() accepts multiple origin strings as parameters (or you can call SetIsOriginAllowed with custom logic for more dynamic matching, like allowing any subdomain pattern), letting a single policy explicitly whitelist several known, trusted origins simultaneously without…

Save .NET mock interview
Advertisement
AModel answer
WithOrigins() accepts multiple origin strings as parameters (or you can call SetIsOriginAllowed with custom logic for more dynamic matching, like allowing any subdomain pattern), letting a single policy explicitly whitelist several known, trusted origins simultaneously without resorting to an insecure wildcard.
builder.Services.AddCors(options => {
    options.AddPolicy("KnownFrontends", policy => {
        policy.WithOrigins(
            "https://app.mycompany.com",
            "https://staging.mycompany.com",
            "http://localhost:3000"  // local dev
        ).AllowAnyMethod().AllowAnyHeader();
    });
});
Real-world example
A team maintains one CORS policy listing their production domain, staging domain, and localhost development URL together, so the same API configuration correctly supports developers, QA testers on staging, and real production users simultaneously.
Likely follow-ups
  • Should localhost origins ever be included in a production CORS policy?
  • How do you manage this origin list across different deployment environments?
Related topics