liveBook · Manning

Cross-origin policy

As explained in Building Web APIs with ASP.NET Core

The same-origin policy is a browser security rule that restricts scripts from accessing resources unless their protocol, host, and port origins match.

The same-origin policy restricts requests made by browser scripts. A page and a requested resource have the same origin only when their protocol, host, and port, if specified, match. A script requesting a resource from another origin needs a mechanism such as CORS. section 3.1.1

A board-game app on another domain

A page at webapp.com loads an Angular app, which requests board games from mybglist-api.com/BoardGames. That API request crosses origins, unlike requests for the page’s own app.js and style.css files. section 3.1.1

To allow the browser app to use the API in ASP.NET Core:

  • Register a CORS policy with AddCors; use WithOrigins to name an allowed origin, or AllowAnyOrigin for an unrestricted policy. section 3.1.1
  • Enable the policy with app.UseCors(), or apply a named policy to selected endpoints with RequireCors. section 3.1.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Defines origin matching and shows CORS policy configuration without detailing browser enforcement internals.
Examples
Uses an Angular app requesting a board-game API on another domain and ASP.NET Core CORS policy listings.
What it assumes
Assumes familiarity with browser HTTP requests and JavaScript apps while defining the origin-matching rule.
What you can do after
Operate/configure it in ASP.NET Core by registering a CORS policy and enabling it through middleware or endpoints.
How it connects
Connects same-origin policy to CORS, HTTP requests, browser scripts, and client-server APIs.

Also explained in

Compare how 2 books explain cross-origin policy →

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.