I am using angular 4 with ASP.NET web api 2 on backend with windows authentication enabled. I have one post and one get api. GET works fine. But for POST I am getting this error
Response for preflight has invalid HTTP status code 401
Which means I am getting unauthorised error for OPTIONS method which I know should not be the case because, OPTIONS method is never called with user credentials.
in angular I am calling api as follows
let requestOptions = new RequestOptions({
withCredentials: true,
headers: new Headers({
'content-type': 'application/json'
})
})
this.http.post("someapiendpoint", JSON.stringify(data), requestOptions).subscribe(data => {
debugger;
});
I have set all CORS header in my web config as follows
<add name="Access-Control-Expose-Headers " value="WWW-Authenticate"/>
<add name="Access-Control-Allow-Origin" value="http://localhost:4200" />
<add name="Access-Control-Allow-Headers" value="accept, authorization, Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Credentials" value="true" />
Also I have following in my authentication block of web.config
<authentication>
<anonymousAuthentication enabled="false" userName="" />
<basicAuthentication enabled="false" />
<clientCertificateMappingAuthentication enabled="false" />
<digestAuthentication enabled="false" />
<iisClientCertificateMappingAuthentication enabled="false">
</iisClientCertificateMappingAuthentication>
<windowsAuthentication enabled="true">
<providers>
<add value="Negotiate" />
<add value="NTLM" />
</providers>
</windowsAuthentication>
</authentication>
I tried removing
headers: new Headers({
'content-type': 'application/json'
})
Following is my controller code for web api
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
namespace Backend.Controllers
{
public class DemoController : ApiController
{
Entities db = new Entities();
public DemoController ()
{
}
[HttpPost]
[ActionName("postevent")]
public IHttpActionResult PostEvent(Demo data)
{
db.Demos.Add(data);
db.SaveChanges();
return Ok();
}
}
}
from my angular code but it did not help. I also tried to replace it with 'text/html' but that did not work too. Can some one tell me what wrong am I doing. As I assume it should work fine if it is working for GET.