2

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;
    }
Gašper Sladič
  • 867
  • 2
  • 15
  • 32
  • This did the trick for anonymous authentication (top voted answer) http://stackoverflow.com/questions/11687711/all-requests-getting-http-error-401-2-unauthorized-response . What about Windows authentication? Do I have to supply some header to HttpClient request? – Gašper Sladič Oct 07 '15 at 10:14
  • You have to pass headers that your web api is looking for. Does your web api have any authorization filter? If yes then you have to check the details what your web api is looking for. – Gagan Jaura Oct 07 '15 at 12:15
  • The UseDefaultCredentials should be sufficient to do Windows Auth from a console app. I just tested it and it worked against a HttpListener hosted server. The AuthenticationManager class takes care of responding to the www-authenticate header with the appropriate Authorization header. – Darrel Miller Oct 07 '15 at 14:49

1 Answers1

0

Try to enable windowsAuthentication in IIS Express in \My Documents\IISExpress\config\applicationhost.config:

<system.webServer>
...
  <security>
...
    <authentication>
      <windowsAuthentication enabled="true" />
   </authentication>
...
  </security>
...
</system.webServer>
Donald
  • 534
  • 1
  • 10
  • 18