|
1 | | -SecureHttpClient |
2 | | -================ |
| 1 | +# SecureHttpClient |
3 | 2 |
|
4 | | -[](https://www.nuget.org/packages/securehttpclient/) |
| 3 | +SecureHttpClient is a dotnet cross-platform HttpClientHandler library, with additional security features. |
5 | 4 |
|
6 | | -SecureHttpClient is a cross-platform HttpClientHandler library, with additional security features: |
7 | | -- certificate pinning |
8 | | -- TLS 1.2+ |
9 | | -- client certificates |
| 5 | +## Features |
10 | 6 |
|
11 | | -Usage: |
12 | | -- basic usage is similar to System.Net.Http.HttpClientHandler. |
13 | | -- for advanced usage examples, look into the SecureHttpClient.Test folder. |
| 7 | +| Feature | Android | iOS | Windows | |
| 8 | +| ---: | :---: | :---: | :---: | |
| 9 | +| Certificate pinning | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
| 10 | +| TLS 1.2+ | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
| 11 | +| HTTP/2 | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
| 12 | +| Compression (gzip / deflate / br) | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
| 13 | +| Client certificates | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
| 14 | +| Headers ordering | :white_check_mark: | :x: | :x: | |
| 15 | +| Cookies | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
14 | 16 |
|
15 | | -Tested on the following platforms: |
16 | | -- Android 5-14 (api 21-34) |
| 17 | +## Installation |
| 18 | + |
| 19 | +[](https://www.nuget.org/packages/SecureHttpClient/) |
| 20 | + |
| 21 | +The most recent version is available (and is tested) on the following platforms: |
| 22 | +- Android 5-14 (API 21-34) |
17 | 23 | - iOS 17.2 |
18 | 24 | - .net 8.0 |
19 | 25 |
|
20 | | -About cookies and redirects: |
21 | | -- SecureHttpClient handles cookies and redirects, but the behavior can differ a bit from one platform to another, because of different implementations in the native libraries used by SecureHttpClient. |
22 | | -- for identical behavior between platforms, it's recommended to use [Flurl](https://github.com/tmenier/Flurl) on top of SecureHttpClient, and let [Flurl](https://github.com/tmenier/Flurl) handle cookies and redirects. |
| 26 | +Older versions support older frameworks (but they are not maintained anymore): |
| 27 | +- v2.1: net7.0 (android / ios / windows) |
| 28 | +- v2.0: net6.0 (android / ios / windows) |
| 29 | +- v1.x: MonoAndroid ; Xamarin.iOS ; NetStandard |
| 30 | + |
| 31 | +## Basic usage |
| 32 | + |
| 33 | +Basic usage is similar to using `System.Net.Http.HttpClientHandler`. |
| 34 | +```csharp |
| 35 | +// create the SecureHttpClientHandler |
| 36 | +var secureHttpClientHandler = new SecureHttpClientHandler(null); |
| 37 | + |
| 38 | +// create the HttpClient |
| 39 | +var httpClient = new HttpClient(secureHttpClientHandler); |
| 40 | + |
| 41 | +// example of a simple GET request |
| 42 | +var response = await httpClient.GetAsync("https://www.github.com"); |
| 43 | +var html = await response.Content.ReadAsStringAsync(); |
| 44 | +``` |
| 45 | + |
| 46 | +## Certificate pining |
| 47 | + |
| 48 | +After creating a `SecureHttpClientHandler` object, call `AddCertificatePinner` to add one or more certificate pinner. |
| 49 | + |
| 50 | +The request will fail if the certificate pin is not correct. |
| 51 | + |
| 52 | +```csharp |
| 53 | +// create the SecureHttpClientHandler |
| 54 | +var secureHttpClientHandler = new SecureHttpClientHandler(null); |
| 55 | + |
| 56 | +// add certificate pinner |
| 57 | +secureHttpClientHandler.AddCertificatePinner("www.github.com", ["sha256/YH8+l6PDvIo1Q5o6varvw2edPgfyJFY5fHuSlsVdvdc="]); |
| 58 | + |
| 59 | +// create the HttpClient |
| 60 | +var httpClient = new HttpClient(secureHttpClientHandler); |
| 61 | + |
| 62 | +// example of a simple GET request |
| 63 | +var response = await httpClient.GetAsync("https://www.github.com"); |
| 64 | +var html = await response.Content.ReadAsStringAsync(); |
| 65 | +``` |
| 66 | + |
| 67 | +In order to compute the pin (SPKI fingerprint of the server's SSL certificate), you can execute the following command (here for `www.github.com` host): |
| 68 | +```shell |
| 69 | +openssl s_client -connect www.github.com:443 -servername www.github.com | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | openssl x509 -noout -pubkey | openssl pkey -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64 |
| 70 | +``` |
| 71 | + |
| 72 | +## Cookies and Redirect |
| 73 | + |
| 74 | +SecureHttpClient handles cookies and redirects, but the behavior can differ a bit from one platform to another, because of different implementations in the native libraries used internally. |
| 75 | + |
| 76 | +For strictly identical behavior between platforms, it's recommended to use [Flurl](https://github.com/tmenier/Flurl) on top of SecureHttpClient, and let it handle cookies and redirects. |
| 77 | + |
| 78 | +```csharp |
| 79 | +// create the SecureHttpClientHandler |
| 80 | +var secureHttpClientHandler = new SecureHttpClientHandler(null); |
| 81 | + |
| 82 | +// disable redirect and cookies management in this handler |
| 83 | +secureHttpClientHandler.AllowAutoRedirect = false; |
| 84 | +secureHttpClientHandler.UseCookies = false; |
| 85 | + |
| 86 | +// create the FlurlClient and CookieSession, they will manage redirect and cookies |
| 87 | +var httpClient = new HttpClient(secureHttpClientHandler); |
| 88 | +var flurlClient = new FlurlClient(httpClient); |
| 89 | +var flurlSession = new CookieSession(flurlClient); |
| 90 | + |
| 91 | +// example of a simple GET request using Flurl |
| 92 | +var html = await flurlSession |
| 93 | + .Request("https://www.github.com") |
| 94 | + .GetStringAsync(); |
| 95 | +``` |
| 96 | + |
| 97 | +## Advanced usage |
23 | 98 |
|
24 | | -Supported frameworks: |
25 | | -- version 1.x: MonoAndroid ; Xamarin.iOS ; NetStandard |
26 | | -- version 2.0: net6.0-android ; net6.0-ios ; net6.0-windows |
27 | | -- version 2.1: net7.0-android ; net7.0-ios ; net7.0-windows |
28 | | -- version 2.2: net8.0-android ; net8.0-ios ; net8.0-windows |
| 99 | +For more advanced usage (logging, client certificates, cookies ordering...), have a look into the SecureHttpClient.Test folder for more code examples. |
0 commit comments