0

I have a WebAPI that sends BASIC authorization information as following.

var client = new HttlpClient();
client.BaseAddress = new Uri(GlobalConstants.LdapUri);
var contentType = new MediaTypeWithQualityHeaderValue("application/json");

client.DefaultRequestHeaders.Accept.Add(contentType);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password))));
Task<HttpResponseMessage> results = client.GetAsync(GlobalConstants.FortressAPIUriDev);
var response = await results;

I've built this API using MVC Core 1.x and the receiving API is built using MVC5.

The problem is that this GetAsync sends two requests at the same time, and I have no clue how to resolve this. I've done some Googling myself to see if I can find a fix for this but so far no luck. Did anyone experience this problem and know how to resolve it?

Thank you very much in advance.

A J
  • 53
  • 1
  • 11

1 Answers1

0

Long story short, found a solution as follows:

using (var client = new HttpClient())
{
    var requestMessage = new HttpRequestMessage(HttpMethod.Get, GlobalConstants.LdapUri + GlobalConstants.FortressAPIUriDev);
    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password))));
    var response = await client.SendAsync(requestMessage);
}

After replacing with this code, it is sending one request at a time. Found a hint at : Adding headers when using httpClient.GetAsync

A J
  • 53
  • 1
  • 11