liveBook · Manning

UseCors

As explained in ASP.NET Core in Action, 3e

UseCors is an ASP.NET Core application-builder method that adds CORS middleware and optionally selects a default policy.

UseCors adds CORS middleware to an ASP.NET Core application. With a policy name, it applies that policy as the application's default CORS policy; without one, endpoint-specific policies can select the rules. section 29.3.2 section 29.3.3

Apply a policy globally

Register an AllowShoppingApp policy for http://shopping.com, then place app.UseCors("AllowShoppingApp") after app.UseRouting(). The middleware permits matching browser requests and adds the headers needed for preflight responses; other origins remain blocked.

UseCors requires a deliberate scope and pipeline position:

  • Place it after UseRouting() so the middleware knows the selected endpoint and can generate correct preflight responses. section 29.3.2 section 29.3.3
  • Choose a global default for an application-wide rule, or call UseCors() without a default policy when endpoints select policies with RequireCors or EnableCors. section 29.3.3
  • Avoid combining a default policy with endpoint policies unless the combined behavior is intentional, because both policies are applied. section 29.3.3
  • Match origins exactly in WithOrigins(), including no trailing slash, or cross-origin requests will fail to match. section 29.3.2
Listing 29.4 Applying a CORS policy to minimal API endpoints
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options => { /* Config not shown*/});
 
var app = builder.Build();
app.UseCors();                                          #1
 
app.MapGet("/api/products", () => new string[] {})
    .RequireCors("AllowShoppingApp");                   #2
 
app.MapGet("/api/products",
    [EnableCors("AllowShoppingApp")] () => new { });    #3
 
app.MapGroup("/api/categories")
    .RequireCors("AllowAnyOrigin");                     #4
 
app.MapDelete("/api/products",
    [DisableCors] () => Results.NoContent());           #5
 
app.Run();
#1 Adds the CorsMiddleware without configuring a default policy
#2 Applies the AllowShoppingApp CORS policy to the endpoint
#3 You can apply attributes to the lamba or handler method, as well as to MVC action methods.
#4 You can apply CORS policies to whole route groups.
#5 The DisableCors attribute disables CORS for the endpoint completely.

Compare middleware without a default policy with endpoint, route-group, and controller-level CORS selection. section 29.3.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains global and endpoint-specific application, middleware ordering, origin matching, and policy-combination risks; defers detailed policy options.
Examples
Uses an AllowShoppingApp policy, a shopping API, minimal API routes, route groups, and a disabled DELETE endpoint.
What it assumes
Assumes basic ASP.NET Core service registration, middleware pipelines, routing, and fluent policy configuration.
What you can do after
Operate/configure it by registering a named policy and placing UseCors correctly for global or endpoint-specific browser access.
How it connects
Connects CORS middleware with endpoint routing, authentication, authorization, RequireCors, and EnableCors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.