Advertisement
Advanced ASP.NET Core, CORS (Cross-Origin Resource Sharing)

How do you configure CORS to allow multiple specific known origins simultaneously, such as production, staging, and local development?

Short answer

WithOrigins() accepts multiple origin strings as parameters (or SetIsOriginAllowed for more dynamic matching), letting a single policy explicitly whitelist several known, trusted origins simultaneously without resorting to an insecure wildcard, commonly pulling the actual list…

Save ASP.NET Core mock interview
Advertisement
AModel answer
WithOrigins() accepts multiple origin strings as parameters (or SetIsOriginAllowed for more dynamic matching), letting a single policy explicitly whitelist several known, trusted origins simultaneously without resorting to an insecure wildcard, commonly pulling the actual list from configuration rather than hardcoding it so different environments can have different allowed origins.
var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>();

builder.Services.AddCors(options => {
    options.AddPolicy("KnownFrontends", policy => {
        policy.WithOrigins(allowedOrigins).AllowAnyMethod().AllowAnyHeader();
    });
});
Real-world example
A team maintains one CORS policy listing their production domain, staging domain, and localhost development URL together, pulled from configuration so each deployed environment can have a different, environment-appropriate allowed origins list.
Likely follow-ups
  • Should localhost origins ever be included in a production CORS configuration?
  • How do you manage this origin list securely across different deployment environments?
Related topics