I have any angular2
app accessing asp.net core webapi
service. It is working if webapi iis configuration is (Properties\launchSettings.json):
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:12246/",
"sslPort": 0
}
},
However, it throws the error once WindowsAuthentication
is true
and AnonymousAuthentication is false
. The error:
XMLHttpRequest cannot load http://localhost:12246/api//values/getSettings. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 401.
Any idea please?
You are attempting to make a cross-origin request. This is permitted under the CORS specification, but requires configuration.
There are three steps to fixing this problem.
Configure both web servers to use Windows Authentication (and disable anonymous authentication). That is, both the server hosting your Angular 2 app and the server hosting your ASP.NET Core WebAPI app must be configured.
Enable CORS your ASP.NET Core WebAPI app:
in your Startup.cs file:
Have Angular 2 send your credentials along with its CORS request:
For further details, see my blog post.