0

I am setting up a new ASP.NET Core Web API with Work Or School Account Authentication. This API will be consumed by an Angular 8.2 application. I added CORS (Microsoft.AspNetCore.Cors - version 2.2.0) from Nuget and configured my Startup.cs file to AddCors and UseCors. I have configured as AllowAnyOrigin, AllowAnyMethod, AllowAnyHeader. My UseCors statement is above UseMVC statement too.

The API works fine when executed independently on web browser. However, when I plug this into the Angular App, I am getting below error.

Access to XMLHttpRequest at 'https://localhost:44342/api/Values' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Why is AllowAnyOrigin is not being recognised?

Can someone help me?

  • What’s the HTTP status code of the response? You can use the Network pane in browser devtools to check. Is it a 4xx or 5xx error rather than a 200 OK success response? – sideshowbarker Oct 03 '19 at 11:36

1 Answers1

1

Below piece of code worked charm for me:

public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }

  app.UseAuthentication();

  app.UseCors(builder => builder
  .AllowAnyOrigin()
  .AllowAnyMethod()
  .AllowCredentials()
  .AllowAnyHeader());
  app.UseMvc();

}

Which i found here.

There are several post around the similar issue , here is the link for the same, please check and let me know if it worked for you.

How does Access-Control-Allow-Origin header work?

Angular app running on azure app service not receive .AspNetCore.Antiforgery cookie sent by .net core web api app on azure

ASP.NET5, MVC 6 Cookies not being shared across sites

Hope it helps, feel free to tag me in any of your conversation.

Mohit Verma
  • 4,531
  • 2
  • 8
  • 25