Browser security prevents a web page from making requests to a different domain than the one that served the web page. This restriction is called the same-origin policy. The same-origin policy prevents a malicious site from reading sensitive data from another site. Sometimes, you might want to allow other sites to make cross-origin requests to your app.
What is the meaning of Same origin?
Two URLs have the same origin if they have identical schemes, hosts, and ports.
These two URLs have the same origin:
https://example.com/foo.html
https://example.com/bar.html
These URLs have different origins than the previous two URLs:
https://example.net: Different domain
https://www.example.com/foo.html: Different subdomain
http://example.com/foo.html: Different scheme
https://example.com:9000/foo.html: Different port
How to enable CORS?
There are three ways to enable CORS:
In middleware using a named policy or default policy.
Using endpoint routing.
With the [EnableCors] attribute.
Warning:UseCors must be called before UseResponseCaching.
publicclassStartup { publicvoidConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddDefaultPolicy( builder => { // Be careful dont use '/' at the end of the url. // eg. 'http://example.com/' does not work correctly. builder.WithOrigins("http://example.com", "http://www.contoso.com");
Enabling CORS with the [EnableCors] attribute and applying a named policy to only those endpoints that require CORS provides the finest control.
The [EnableCors] attribute provides an alternative to applying CORS globally. The [EnableCors] attribute enables CORS for selected endpoints, rather than all endpoints:
[EnableCors] specifies the default policy.
[EnableCors("{Policy String}")] specifies a named policy.
The [EnableCors] attribute can be applied to:
Razor Page PageModel
Controller
Controller action method
Different policies can be applied to controllers, page models, or action methods with the [EnableCors] attribute. When the [EnableCors] attribute is applied to a controller, page model, or action method, and CORS is enabled in middleware, both policies are applied. We recommend against combining policies. Use the [EnableCors] attribute or middleware, not both in the same app.
[EnableCors("MyPolicy")] [Route("api/[controller]")] [ApiController] publicclassValuesController : ControllerBase { // GET api/values [HttpGet] public IActionResult Get() => ControllerContext.MyDisplayRouteInfo();
// GET api/values/5 [HttpGet("{id}")] public IActionResult Get(int id) => ControllerContext.MyDisplayRouteInfo(id);
// PUT api/values/5 [HttpPut("{id}")] public IActionResult Put(int id) => ControllerContext.MyDisplayRouteInfo(id);
// GET: api/values/GetValues2 [DisableCors] [HttpGet("{action}")] public IActionResult GetValues2() => ControllerContext.MyDisplayRouteInfo();
}
CORS policy options
AllowAnyOrigin: Allows CORS requests from all origins with any scheme (http or https). AllowAnyOrigin is insecure because any website can make cross-origin requests to the app.
AllowAnyMethod: Allows any HTTP method.
AllowAnyHeader: Ensures that the policy allows any header.
AllowCredentials: The server must allow the credentials.