I am trying to implement a CORS Policy in my ASP.NET Core Web API.
My Startup.cs file is as follows:
public void ConfigureServices(IServiceCollection services)
{
options.AddPolicy("AllowSpecificOrigin", builder => builder.WithOrigins("https://google.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowAnyOrigin());
services.AddMvc()
.AddApplicationPart(Assembly.Load(new AssemblyName("Proxy.Api.Application")))
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.WriteIndented = true;
});
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors("AllowSpecificOrigin");
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
I also added the controller method decorator attribute below:
[EnableCors("AllowSpecificOrigin")]
public Task<IActionResult> GetProxy([FromQuery] ProxyParams inputRequest)
{
....
}
Now to my understanding, this line:
options.AddPolicy("AllowSpecificOrigin", builder => builder.WithOrigins("https://google.com")
will only allow https://google.com domain to access my controller method with the EnableCors attribute decorator.
However, when I call the API endpoint locally through curl or fiddler, the method is still being hit and is returning 200. What am I missing? I've checked several posts but I'm kind of stuck.