Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling multiple CORS for Web API

I have a Web API I wrote and one application utilizing it. SO I added CORS header for that applicaiton by adding a header to the controller class within my API:

[EnableCors(origins: "http://localhost:59452", headers: "*", methods: "*")]

The above worked fine. Now I also want more applications consuming that web API. My question is how do I make this happen?

like image 340
Coding Duchess Avatar asked Feb 02 '16 15:02

Coding Duchess


People also ask

How do I fix the CORS issue in Web API?

Say the front-end is served from https://demodomain-ui.com and the backend is served from from https://demodomain-service.com/api. If an end user tries to access the application, for security reasons the browsers restrict cross-origin HTTP requests initiated from the UI. The first is to install the Microsoft. AspNet.

Should API enable CORS?

CORS is typically required to build web applications that access APIs hosted on a different domain or origin. You can enable CORS to allow requests to your API from a web application hosted on a different domain.

What is the CORS issue in Web API?

Cross-origin resource sharing (CORS) is a browser security feature that restricts cross-origin HTTP requests that are initiated from scripts running in the browser. If your REST API's resources receive non-simple cross-origin HTTP requests, you need to enable CORS support.


1 Answers

You can add multiple origins by separating them with commas:

[EnableCors(origins: "http://localhost:59452,http://localhost:25495,http://localhost:8080", headers: "*", methods: "*")]
like image 132
Sean Bright Avatar answered Oct 05 '22 00:10

Sean Bright