4

I am writing a .NET Core Web API that utilizes HttpClient to communicate with an external API from my work network. Initially I was having intermittent 407 Proxy Authentication required errors and came across this article as I was incorrectly passing credentials to the web server instead of the proxy.

I made the changes described in the article, and now I'm getting an exception from our proxy server:

"Your credentials could not be authenticated: Credentials are missing. You will not be permitted access until your credentials can be verified."

Oddly enough, I tested the code below (on the same machine) using a simple .NET Framework 4.6.1 console application and it works just fine as I am getting the expected payload from the external API. I have the exact same code for setting up my HttpClient in the Web API and cannot figure out why it appears that the credentials are not being passed along to the WebProxy accordingly.

Note: I have tried hard coding a specific username and password for the WebProxy.Credentials (as shown in the linked article above), presuming that the authority that runs the Web API in the debugger on my local machine may not be the same as when running in a console application. I still get the above message about credentials missing.

var httpClientHandler = new HttpClientHandler
{
    UseProxy = true,
    Proxy = new WebProxy()
    {
        Address = new Uri("http://companyproxy:port"),
        BypassProxyOnLocal = true,
        UseDefaultCredentials = false,
        Credentials = CredentialCache.DefaultCredentials
        //Credentials = new NetworkCredential("userName", "password")
    }
};

HttpClient httpClient = new HttpClient(httpClientHandler)
{
    BaseAddress = new Uri("http://externalApiUrl/endpoint")
};
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Credentials you are passing in WebProxy are used for authentication against the WebProxy. If you also have to authenticate against the actual API, you need to pass those credentials separately as part of httpClient. – Chetan Nov 30 '18 at 03:34
  • I am aware of this. I did not include the code snippet where I am attaching the DefaultRequestHeaders for the external API to my HttpRequest, as I already know the authentication against the external API is working via the test I ran in the console application. – franzpaniero Nov 30 '18 at 03:50

0 Answers0