Cross-origin requests (CORS)
Browsers block JavaScript on one site from reading responses from another site unless the server allows it. This lesson explains the same-origin policy, how CORS lets your API opt in for specific origins, preflight requests, and the configuration mistakes that open security holes.
- StartPage on shop.exampleJavaScript calls api.shop.example
- Step 2Preflight OPTIONSBrowser asks if the origin, method and headers are allowed
- Step 3CORS middlewareChecks the Frontend policy
- Step 4AllowedReal request sent; JavaScript reads the response
- EndNot allowedBrowser blocks it and logs a CORS error
The same-origin policy
An origin is scheme + host + port. https://shop.example and https://api.shop.example are different origins. By default a browser lets a page send a request to another origin but will not let its JavaScript read the response.
CORS (Cross-Origin Resource Sharing) is how the API tells the browser "this origin is allowed" through Access-Control-Allow-* headers. It protects users in browsers; it is not authentication and does nothing against curl or server-to-server calls.
Configuring a policy
builder.Services.AddCors(o => o.AddPolicy("Frontend", p => p
.WithOrigins("https://shop.example", "http://localhost:5173")
.WithMethods("GET", "POST", "PUT", "DELETE")
.WithHeaders("Content-Type", "Authorization")
.SetPreflightMaxAge(TimeSpan.FromMinutes(10))));
var app = builder.Build();
app.UseRouting();
app.UseCors(); // after UseRouting, before UseAuthorization
app.UseAuthorization();
app.MapControllers().RequireCors("Frontend"); // or [EnableCors("Frontend")] per controllerPreflight requests
For requests with JSON bodies, custom headers or methods such as PUT and DELETE, the browser first sends an OPTIONS request asking for permission. The CORS middleware answers it. If the preflight fails, the real request is never sent, and the browser console shows a CORS error.
OPTIONS /api/orders HTTP/1.1
Origin: https://shop.example
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type, authorization
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://shop.example
Access-Control-Allow-Methods: GET,POST,PUT,DELETE
Access-Control-Allow-Headers: Content-Type,AuthorizationMistakes to avoid
- AllowAnyOrigin() on an API that uses cookies or tokens lets any website call it from a signed-in user's browser. List origins explicitly.
- AllowAnyOrigin cannot be combined with AllowCredentials; ASP.NET Core refuses it.
- Do not reflect whatever Origin header arrives; that is AllowAnyOrigin in disguise.
- If the site and API share an origin (the usual MVC case), you do not need CORS at all.
Interview question
Does CORS protect your API from unauthorized callers?
No. CORS is enforced by browsers to protect users: it decides whether JavaScript from another origin may read your responses. Tools like curl, Postman or another server ignore it completely. APIs still need authentication and authorization; CORS just controls which websites can use them from a user's browser.
Want the full details? Read the official Microsoft documentation for this topic.