I have a simple Angular App running locally on 4200 i am testing with a web api running locally on 5000 using .net core. My startup.cs has CORS configured correctly to allow everything i believe.
In the ConfigureServices section i have:
services.AddCors();
In the Configure section i have:
app.UseCors(options => options.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
However when i try and hit my webapi i still get this in the browser.
from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
I have looked at the other answers and they all seem to have this or similar variations that i have tried with no change. Not sure what else i need to do?
Add the following code snippet in the method ConfigureServices. Edit this for allowing only custom headers.
Add the following in Configure method
Add 'EnableCors' attribute to the controller.
I ran in the same problem but i my case i had windowsauthentification.
@RandyDaddis pointed me to the right direction. You can't use '*' if authentification is set.
But this doesn't solve the hole problem (and for me '*' is not a good option).
I had to changed from windowsAuthentication only to a mixed mode (where anonymousAuthentication is also enabled) because the preflight was not sending the credentials.
This was already an discussed issue by asp.
Important is that you add this to your startup.cs in the ConfigureServices function to ensure all your controllers are still under authorization-policy:
Register cors in services
add cors middleware
Possible Issues:
startup.cs.Configure()
method, doesapp.UseCors()
precedeapp.useMVC()
?(/)
?Gotchas
Firefox requires a certificate to be installed for your API in order to send Http Request utilizing the HTTPS protocol.
Test your API with Postman and your browser Developer Tool. Notice 2 Http requests. The Http 200 is a “pre-flight” to see what CORS options are available.
HTTP 500 (Internal Server Error)
, it will return a developer exception page and Postman will display a“no ‘Access-Control-Allow-Origin’ header is present on the requested resource”
message - this is mis-leading.I would like to respectfully point out the following:
AllowAnyOrigin()
is not recommended in production unless you intend to allow anyone to utilize your API AND you will not implement credentials.EnableCors
attribute when utilizing multiple CORS policies.The following articles are worth reviewing:
ASP.NET Core 2.2 does not permit allowing credentials with AllowAnyOrigin()
Enable Cross-Origin Requests (CORS) in ASP.NET Core
key points (tldr;):
(/)
.CORS Tutorial: (2) Angular Clients + ASP.NET Core
Create Visual Studio Solution
Create ASP.NET Core Project
API Launch Settings and CORS Configuration
launchSettings.json
startup.cs
Set breakpoint on
ValuesController.Get()
Test API with Postman:
https://localhost:myApiPortNumber/api/values
Create Angular application
Start Spa1 application
Browse to http://localhost:4200/
Implement CORs in Spa1
app.module.ts
app.component.ts
app.component.html
environment.ts
Start Spa1 application
Browse to http://localhost:4200/
One way of remediating the Firefox block:
CORS Test
Clone Spa1
Refactor title of Spa2
app.component.ts
Start Spa2 application on port 3200
Browse to http://localhost:3200/
Stop Debugging API with Development (IIS Express) profile
Start Debugging API with Staging (IIS Express) profile
Browse to http://localhost:4200/
Browse to http://localhost:3200/
inspect Http Response with Developer Tools: