liveBook · Manning

Preflight request

As explained in Building Web APIs with ASP.NET Core

A preflight request is an HTTP OPTIONS request that a browser sends to determine whether a subsequent cross-origin request is permitted.

A preflight request is an HTTP OPTIONS request a browser sends before certain cross-origin requests. If a script’s request does not qualify as a simple request, the browser puts it on hold and uses the preflight request to check the server’s CORS capabilities. section 3.1.1

The browser enforces the same-origin policy; the server supplies CORS settings for the browser to check. A simple request goes directly to the resource, while a preflight adds a separate check before that request proceeds. section 3.1.1

builder.Services.AddCors(options =>
   options.AddDefaultPolicy(cfg => {
       cfg.AllowAnyOrigin();
       cfg.AllowAnyHeader();
       cfg.AllowAnyMethod();
   }));

For an ASP.NET Core API, this listing registers a default CORS policy that allows any origin, header, and method. Those settings are permissive; choose what the API should allow rather than treating them as a required preflight configuration. section 3.1.1

// ...
 
app.UseCors();
 
app.UseAuthorization();
 
// ...

This listing places app.UseCors() in the request pipeline before authorization, enabling the registered CORS policy. section 3.1.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines when a browser sends HTTP OPTIONS and why, without detailing the requirements for a simple request.
Examples
Shows ASP.NET Core CORS policy registration and middleware placement.
What it assumes
Introduces the same-origin policy and simple requests; assumes familiarity with basic HTTP requests.
What you can do after
Operate/configure CORS in ASP.NET Core using a registered policy and middleware, while recognizing why a browser sends a preflight request.
How it connects
Connects preflight requests to CORS, the same-origin policy, simple requests, and HTTP OPTIONS.

Also explained in

Compare how 3 books explain preflight request →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.