your application to specify which domains are permitted to access its resources. By default, web browsers enforce the same-origin policy, which restricts web pages from making requests to a domain different from the one that served the web page. CORS is used to relax this restriction and allow cross-origin requests in a controlled and secure manner.
When a web page on Domain A tries to access resources (like APIs) from Domain B, the browser checks the CORS policy set by Domain B. CORS is implemented by adding specific HTTP headers to the server response.
Here is why CORS is important.
Access to XMLHttpRequest at 'http://example.com/api' from origin 'http://another-origin.com' has been blocked by CORS policy.
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://contoso.example.com/foo.html: Different subdomain
http://example.com/foo.html: Different scheme
https://example.com:9000/foo.html: Different port
Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS
policy
.
https://app.example.com
and backend API on
https://api.example.com
).Here's how you can enable and configure CORS in a .NET Core application.
After installed Cors. We can check.
If you are using .NET Core 2.x, ensure you have the required package:
You need to add and configure CORS middleware in the service container.
builder.Services.AddAuthorization();
builder.Services.AddScoped<IMaterialService, MaterialService>();
builder.Services.AddSwaggerGen();
// Adding CORS services with a policy
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigins", builder =>
{
builder.WithOrigins("https://example.com", "https://anotherdomain.com") // Allow specific domains
.AllowAnyHeader() // Allow any header
.AllowAnyMethod(); // Allow any HTTP method (GET, POST, etc.)
});
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin() // Allow all domains
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddControllersWithViews();
Use the CORS middleware in Program.cs file
app.UseRouting();
// Enable CORS globally or for specific endpoints
app.UseCors("AllowSpecificOrigins");
app.UseAuthentication();
You can apply the CORS policy to specific controllers or actions using the [EnableCors] attribute.
a. For a specific controller
[EnableCors("AllowSpecificOrigins")]
[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Ok("CORS is enabled for specific origins.");
}
}
b. Disable CORS for a specific action
[DisableCors]
[HttpGet("no-cors")]
public IActionResult NoCors()
{
return Ok("CORS is disabled for this action.");
}
Once you've set up CORS, you can test it by making requests to your API from a different domain and verifying that the responses are successfully received.
The CorsPolicyBuilder provides several methods to configure CORS behavior: