0

I have got an MVC 5 web application hosted on IIS8 which is secured with Windows Authentication. The application has an WebApi2 controller which I am calling server side from itself via an MVC controller, however I keep getting a 401 Unauthorized when making the call (POST verb).

var client = new HttpClient();
var response = client.PostAsJsonAsync(url, dto).Result;

Things I have tried with no success:

Wondering if anyone else has any suggestions/seen this issue before?

Rob C
  • 818
  • 1
  • 12
  • 26
  • How are you making the call? If you're using http to invoke another controller method, you need to send the authorization headers. –  Jun 07 '17 at 14:24
  • I have updated the question with the code I am using to call the api endpoint – Rob C Jun 07 '17 at 14:57
  • 1
    Okay, yes, that's an HTTP call. The ASP.Net security context does not translate over HTTP, so you need to add the appropriate authorization headers. –  Jun 07 '17 at 14:59
  • Ah ok thanks Amy. I will look into it :) – Rob C Jun 07 '17 at 15:00

1 Answers1

0

This solution worked for me when instantiating my HttpClient.

            var clientHandler = new HttpClientHandler()
            {
                Credentials = CredentialCache.DefaultNetworkCredentials
            };
            var client = new HttpClient(clientHandler);
Rob C
  • 818
  • 1
  • 12
  • 26