0

My requests have been blocked by CORS policies, I have the following in my startup class:

         services.AddCors(options =>
        {
            options.AddDefaultPolicy(
                builder => {
                    builder
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowCredentials();
                });
        });

I'm using an Nginx server and I always get the following error when I make a request with my angular app:

has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Any help is really appreciated!

Edit

I made some changes suggested in the code, but I got the same error. The changes made were the following:

         services.AddCors(options =>
        {
            options.AddPolicy(
                "corsapp",
                builder => {
                    builder
                        .WithOrigins("https://myurl/", "https://myurl2/")
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        .AllowCredentials();
                });
        });

And in the Configure method:

        app.UseRouting();
        app.UseCors("corsapp");

        app.UseAuthorization();

myurl and myurl2 are two urls that make requests to the api, both have the same domain, but different subdomain

11
  • which version of .net core are you using? Commented Mar 29, 2022 at 21:45
  • I'm using net core 6 Commented Mar 29, 2022 at 21:49
  • check this link out: stackoverflow.com/questions/70511588/… It basically says create a policy, and then add app.UseCors("THE_POLICY_YOU_CREATED"); Commented Mar 29, 2022 at 21:53
  • I just tried that out, and I got the same response :( Commented Mar 29, 2022 at 22:04
  • make sure you write app.UseCors("WHATEVER") before you use app.MapControllers() or app.UseMvc() Commented Mar 29, 2022 at 22:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.