I am getting below CORS errors for PUT and DELETE request only, GET and POST requests are working fine:
Access to XMLHttpRequest at 'https://localhost:444/api/...' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Background about my application: frontend is in React(next.js) and backend is in .NET 5. In backend project, I have already set the CORS policy to allow any header, method and specific origin but still now working. When I run the API project on IIS Express, it works but when I deployed it to IIS, it failed with above error.
Here are the CORS policy in .NET 5 project:
-- public void ConfigureServices(IServiceCollection services)
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder
.AllowAnyHeader()
.AllowAnyMethod()
.WithOrigins("http://localhost:3000");
});
-- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseRouting();
app.UseCors();
app.UseAuthorization();
Here is the OPTIONS request for PUT:
**General:**
Request Method: OPTIONS
Status Code: 204
Remote Address: [::1]:444
Referrer Policy: strict-origin-when-cross-origin
**Response Headers:**
access-control-allow-headers: content-type
access-control-allow-methods: PUT
access-control-allow-origin: *
date: Fri, 24 Sep 2021 01:49:11 GMT
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
Any idea?
I believe when you deploy your code the client url will change from http://localhost:3000 to say [http://example.com/8080][1] Put this url in WithOrigins method -