CORS policy does not work in .net 6.0 WebAPP and WebAPI

Hi guys,
I have a solution with 2 projects. A .net 6.0 webAPP and webAPI, the web APP using JQuery to post to the webAPI, however I keep getting CORS policy has been blocked.

I enabled it in the program.cs file in the WebAPP file as below, but does not seem to work. Please help

builder.Services.AddCors(options =>
{
    options.AddPolicy("VM_CorsPolicy", builder => {
        builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod();
    });
});



// Added after routing and before end points methods
app.UseCors("VM_CorsPolicy");

webAPP or webAPI? Typically CORS is governed by the server so I would think this belongs on the webAPI project. A request is made (either by your webAPP or some other request from someone else) and the server/api determines if the origin in the request is compatible with the policy. If so, then it responds with the resource.

So just to make sure, you are applying CORS logic to your webAPI endpoints (the project offering the resources) right?

There are 2 projects, a web APP posting to a web API

CORS (which I pointed out in my code above) is applied to both projects in the program.cs file for the .net core 6 project(s)

You have verified that the CORS headers are being added to the response using curl or something to that effect without the CORS constraint?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.