I have two applications. One is ASP.NET Web Api and the other is console application from which I call the API. When debugging I always get the 401.2 response. Fiddler gives back this response
You are not authorized to view this page due to invalid authentication headers.
My question is how to configure IIS Express and how to set the header in the console application to call the Web Api properly? I would like to make Windows or Anonymous authentication. I also enabled Windows authentication in IIS Express.
Console application code:
MachineStateModel model = new MachineStateModel();
model.DataType = "1";
model.MachineID = 1;
model.TimeStamp = DateTime.Now;
model.Value = "0";
HttpClientHandler handler = new HttpClientHandler() { UseDefaultCredentials = true };
using (var Client = new HttpClient(handler))
{
Client.BaseAddress = new Uri("http://localhost.fiddler:55308/");
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await Client.PostAsJsonAsync("api/machinestate", model);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Call is successful");
}
}
ASP.NET Web Api web.config:
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<customErrors mode="Off" />
<authentication mode="Windows"/>
<authorization>
<allow users="?"/>
</authorization>
ASP.NET Web Api Controller method:
// POST api/values
public HttpResponseException Post([FromBody]MachineStateModel value)
{
XmlMStateStream CurrentStream = new XmlMStateStream();
CurrentStream.DateTime = value.TimeStamp;
CurrentStream.MachineID = value.MachineID;
CurrentStream.DataType = value.DataType;
CurrentStream.Value = value.Value;
HttpResponseMessage responsemessage = Request.CreateResponse(HttpStatusCode.OK, CurrentStream);
HttpResponseException response = new HttpResponseException(responsemessage);
return response;
}