Introduction
Requirements for Enabling CORS
- Install the CORS middleware.
- Register CORS middleware to the pipeline in the ConfigureServices method of Startup.cs.
- Enable CORS in the Configure method of Startup.cs.
- Enable/Disable CORS in the controllers, the action methods, or globally.

// In general
services.AddCors();
with a Default policy,
// Default Policy
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("https://localhost:44351", "http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
or with a named policy,
// Named Policy
services.AddCors(options =>
{
options.AddPolicy(name: "AllowOrigin",
builder =>
{
builder.WithOrigins("https://localhost:44351", "http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
Step 3 - Enable CORS in the Configure method of Startup.cs
// in general
app.UseCors();
Define a policy,
// Shows UseCors with CorsPolicyBuilder.
app.UseCors(builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
or with a named policy,
// with a named pocili
app.UseCors("AllowOrigin");
Note
- You have to register the CORS in ConfigureService method, and enable CORS in Configure Method, and you could define the policy in both places. However:
- You have to define the policy in at least one place;
- The call to
UseCorsmust be placed afterUseRouting, but beforeUseAuthorization. For more information, see Middleware order, or the graph below here:

[EnableCors]or[DisableCors]specifies the default policy.[EnableCors("{Policy String}")]specifies a named policy.[EnableCors("AllowOrigin")] // GET: api/StoresWebAPI [HttpGet] public async Task<ActionResult<IEnumerable<Store>>> GetStores() { return await _context.Stores.ToListAsync(); }
Cases for Enabling CORS
public void ConfigureServices(IServiceCollection services)
{
// In general
services.AddCors();
......
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
// Shows UseCors with CorsPolicyBuilder.
app.UseCors(builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
......
}
Default Policy in ConfigureService Method: Working
public void ConfigureServices(IServiceCollection services)
{
// Default Policy
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("https://localhost:44351", "http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
......
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
app.UseCors();
......
}
Named Policy in ConfigureService Method: Working
public void ConfigureServices(IServiceCollection services)
{
// Named Policy
services.AddCors(options =>
{
options.AddPolicy(name: "AllowOrigin",
builder =>
{
builder.WithOrigins("https://localhost:44351", "http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
......
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
app.UseCors();
......
}
// Must Enable CORS from Action or Controller level
[EnableCors("AllowOrigin")]
// GET: api/StoresWebAPI
[HttpGet]
public async Task<ActionResult<IEnumerable<Store>>> GetStores()
{
return await _context.Stores.ToListAsync();
}
Combined Definitions of Policies: Working
public void ConfigureServices(IServiceCollection services)
{
// Default Policy
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("https://localhost:44351", "http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// Named Policy
services.AddCors(options =>
{
options.AddPolicy(name: "AllowOrigin",
builder =>
{
builder.WithOrigins("https://localhost:44351", "http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
......
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
app.UseCors();
// with a named pocili
app.UseCors("AllowOrigin");
// Shows UseCors with CorsPolicyBuilder.
app.UseCors(builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
......
}
Without definition of a policy: Not Working
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
......
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
app.UseCors();
......
}
Note
Here, we use angular client developed in Part II to do our tests, but not the MVC client we devbeloped in Part I. Actually, by my test, the MVC Client is automatically to have access to Web API different origin source, that we only need to enable CORS from Config method like below,
policy: Not Working
public void ConfigureServices(IServiceCollection services)
{
......
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
......
app.UseCors();
......
}
I do not know the reason, and I cannot get any explanation from online. Anyone has knowledge about that, I will appreciate your input.
Summary
《Enable Cross-Origin Requests (CORS) in ASP.NET Core》--- MS
《How to Enable Cross-Origin Requests (CORS) in ASP.NET Core》--- Yogi Hosting
《Enable CORS In ASP.NET WebAPI 2》--- c-sharpcorner.com
《Enable cross-origin requests in ASP.NET Web API 2》--- MS
《How to Enable CORS in the ASP.NET Web API》--- enable-cors.org

Dinand HartPosted Dec 28, 2022, 7:12 PM
For .net 7 Blazor server users: No need to install the nuget package, just add step 3 (no named policy is needed, but adviced), and it will work.