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")
};