HttpClientHandler UseDefaultCredentials in .NET Core

Viewed 1330

With the following running in a .NET Framework 4.7.2 Console application and some_url pointing to a server in my Windows network that uses Kerberos, I get a HTTP code of 200 back:

var handler = new HttpClientHandler { UseDefaultCredentials = true };
var client = new HttpClient(handler);

var code = client.GetAsync(some_url).Result.StatusCode

but using the same code in a .NET Core 3.1 application gives a 401 instead. This is the same as I get when running in .NET Framework 4.7.2 but not setting UseDefaultCredentials to true, leading me to believe the .NET Core 3.1 version does not pass the credentials along.

What do I need to do to get a .NET Core 3.1 application to pass the current credentials? I've tried playing around with setting the Credentials on the handler to an instance of NetworkCredential but to no avail.

1 Answers

you should change AllowAutoRedirect to false and loop over the response message

HttpResponseMessage httpResponse;
do
{
   httpResponse = await httpClient.GetAsync(uri);
   if (httpResponse.Headers.Contains("Location"))
   {
       //Find the redirection address
        uri = httpResponse.Headers.Location.OriginalString;
   }
   else
   {
       throw new ApplicationException("No Location header found in web response");
   }
 } while (httpResponse.StatusCode == HttpStatusCode.Redirect);
Related