1

Why can't I add authentication details after adding a DelegatingHandler?

Prior to adding the DelegatingHandler, I was able to instantiate the HttpClient like so:

var client = new HttpClient{Credentials = ..., BaseAddress = ...};

After having added a DelegatingHandler, I am unable to add Credentials and BaseAddress:

var client = new HttpClient(new RetryHandler(new HttpClientHandler()));
client.BaseAddress = // does not exist!
client.Credentials = // does not exist!

How do we add credentials and a baseaddress to this client?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

1 Answers1

2

This is how you can pass credentials into your handler:

var clientHandler = new HttpClientHandler()
{
    Credentials = ...
};

var retryHandler = new RetryHandler(clientHandler);

var client = new HttpClient(retryHandler )
{
    BaseAddress = ...
};
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Alex AIT
  • 17,361
  • 3
  • 36
  • 73