Intermittent issue while trying to fetch windows credentials when UseDefaultCredentials = true in HttpClientHandler

Yandra Divya Brahmani 0 Reputation points
2024-09-03T06:04:04.3933333+00:00

Hi,

In our application we are trying to use windows credentials while fetching data from an API. Below is the code we have implemented. This is giving intermittent issues while fetching DefaultCredentials. We observed in fiddler that Authorization header("Authorization: Negotiate <token>") is not getting created for this call. This issue is occurring intermittently. Could someone suggest a solution here? Are we missing anything?

We are using net8.0

var handler = new HttpClientHandler
 {
     UseDefaultCredentials = true
 };
 var client = new HttpClient(handler)
 {
     Timeout = new TimeSpan(0,0,1,0)
 };
 client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
 client.DefaultRequestHeaders.Add("x-bwin-client-machine", Environment.MachineName);
var response = await client.GetAsync(url);
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,526 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 64,481 Reputation points
    2024-09-04T01:58:39.86+00:00

    With window authentication, on every connection, a request is made, and a 401 response with negotiation supported is received. Then the client sends a negotiation token. Then an out of band challenge/ response is done. Typically you would use keep-alive so multiple request can be made without additional authentication.

    you are not following best practices, which may be casing your issues. You should be using pooled connections and a static httpclient, not creating a new httpclient for each request. See

    https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.