I'm trying to access a file in a web site using HttpClient, but I'm getting 401 Unauthorized.
The web site is configured with
<authentication mode="Windows" />
...
<authorization>
<allow users="*" />
</authorization>
And the IIS is configured for Windows Authentication only.
I'm able to access the file using Internet Explorer by specifing the address in the URL and I'm NOT prompted for username/password.
When trying to do the "same" using HttpClient, I get 401 Unauthorized.
var httpClientHandler = new HttpClientHandler { UseDefaultCredentials = true };
using (var httpClient = new HttpClient(httpClientHandler))
{
var response = httpClient.GetAsync("http://example.com/welcome.png");
// The response is 401 Unauthorized.
}
What am I doing wrong?
UPDATE:
It works when using Basic Authentication and specifying httpClientHandler.Credentials = new NetworkCredential("DOMAIN\my.name", "mypassword"), but it goes without saying that that is not an option. Windows authentication and not having to specify username / password is desired.