I have separate projects for front-end (Angular) and back-end (.NET Core WEB Api). I have been setup the CORS as well the windows AD authentification. All that is working well for GET calls, but I am having issues with the POST. I am getting following error.
OPTIONS http://localhost:50000/api/data/Update 401 (Unauthorized)
Access to XMLHttpRequest at 'http://localhost:50000/api/data/Update' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Error: Http failure response for http://localhost:50000/api/data/Update: 0 Unknown Error
user.component.ts
update(data: NgForm) {
if (data.form.valid) {
this.service.update(data.value)
.subscribe(
res => {
console.log('Result: ' + res);
},
err => {
console.log('Error: ' + err.message);
});
}
}
service.ts
headerOptions = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded', 'Access-
Control-Allow-Origin': '*' });
update(data: Form) {
return this.http.post(this.apiUrl + '/data/Update', data, { headers: this.headerOptions });
}
Controller
[HttpPost("[action]")]
public string Update([FromForm] User data)
{
if (data != null)
{
return "Ok";
}
else
{
return "Data is null";
}
}
CORS extension
public static void ConfigureCors(this IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
}
I also have the interceptor inside Angular which is sending "withCredentials: true" for all calls.
Have in mind that all my GET calls are working without issues, I only have problems with the POST. If I change the method on Angular side from POST to GET, the Update method from back-end is getting called without issues...