ASP.NET Core Interview Question #96
What is CORS middleware in ASP.NET Core?
Middleware & Request Pipeline Mid-Level Intermediate
Quick Interview Answer
Cross-Origin Resource Sharing (CORS) controls whether browser-based code from one origin is allowed to call resources hosted on another origin.
Detailed Explanation
Cross-Origin Resource Sharing (CORS) controls whether browser-based code from one origin is allowed to call resources hosted on another origin.
ASP.NET Core provides CORS services and middleware for defining allowed origins, methods, headers, and credential behavior. A restrictive named policy is generally safer than allowing every origin.
CORS is a browser security mechanism; it is not authentication or authorization and does not prevent non-browser clients from sending HTTP requests.
Code Example
builder.Services.AddCors(options =>
{
options.AddPolicy("Frontend", policy =>
{
policy.WithOrigins("https://app.example.com")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
var app = builder.Build();
app.UseCors("Frontend");