109

I'm currently using HttpWebRequest to get a website. I'd like to use the await pattern, which is not given for HttpWebRequests. I found the class HttpClient, which seems to be the new Http worker class. I'm using HttpClient.GetAsync(...) to query my webpage. But I'm missing the option to add ClientCredentials like HttpWebRequest.Credentials. Is there any way to give the HttpClient authentication information?

Andrew Carmichael
  • 3,086
  • 1
  • 22
  • 21
Jan K.
  • 2,542
  • 2
  • 21
  • 30

2 Answers2

184

You can pass an instance of the HttpClientHandler Class with the credentials to the HttpClient Constructor:

using (var handler = new HttpClientHandler { Credentials = ... })
using (var client = new HttpClient(handler))
{
    var result = await client.GetAsync(...);
}
dtb
  • 213,145
  • 36
  • 401
  • 431
  • 22
    You can also set `UseDefaultCredentials = true` for `HttpClientHandler` – Aleksei Poliakov Jan 26 '14 at 14:13
  • 9
    This can cause suboptimal behavior when *Basic* authentication is required http://stackoverflow.com/q/25761214/57428 – sharptooth Sep 10 '14 at 09:38
  • 2
    i've found you will want to set handler.ClientCertificateOptions = ClientCertificateOption.Automatic in order to actually have the credititals sent. – Garr Godfrey Feb 29 '16 at 00:10
  • 6
    Its advisable to use a static instance of HttpClient, especially in server scenarios – James Westgate Jul 27 '17 at 13:26
  • 3
    So what should we do in server scenarios? We are running in to the issues that you have when you don't have a static instance but we need to pass in credentials. – Scott Chamberlain Sep 07 '17 at 14:56
  • 1
    @ScottChamberlain in that case, you need to maintain a small pool of `HttpClient` instances, one for each specific scenario. The recommendation is to reuse a single instance "whenever possible". If it is not possible to reuse the very same instance across the entire app, it is fine to create a few dedicated ones. They should still be singletons for that context though. – julealgon Dec 20 '18 at 12:25
  • 2
    How can this be done when using a named HttpClient provided by IHttpClientFactory in ASP.NET Core? – Valuator Feb 28 '19 at 21:37
6

You shouldn't dispose of the HttpClient every time, but use it (or a small pool of clients) for a longer period (lifetime of application. You also don't need the handler for it, but instead you can change the default headers.

After creating the client, you can set its Default Request Headers for Authentication. Here is an example for Basic authentication:

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "username:password".ToBase64());

ToBase64() represents a helper function that transforms the string to a base64 encoding.

JSilvanus
  • 105
  • 1
  • 7
  • 1
    this way works much better when using a HttpClientFactory, allows you to easily use the same httpClient with different credentials without creating a new handler each time. – dove Jan 11 '22 at 11:20