Providing your own HttpClient and proxy using MSAL.NET

When initializing a public client application, you can use the .WithHttpClientFactory method to provide your own HttpClient. Providing your own HttpClient enables advanced scenarios such fine-grained control of an HTTP proxy, customizing user agent headers, or forcing MSAL to use a specific HttpClient (for example in ASP.NET Core web apps/APIs).

Initialize with HttpClientFactory

The following example shows to create an HttpClientFactory and then initialize a public client application with it:

IMsalHttpClientFactory httpClientFactory = new MyHttpClientFactory();

var pca = PublicClientApplicationBuilder.Create(MsalTestConstants.ClientId) 
                                        .WithHttpClientFactory(httpClientFactory)
                                        .Build();

HttpClient and Xamarin iOS

When using Xamarin iOS, it is recommended to create an HttpClient that explicitly uses the NSURLSession-based handler for iOS 7 and newer. MSAL.NET automatically creates an HttpClient that uses NSURLSessionHandler for iOS 7 and newer. For more information, read the Xamarin iOS documentation for HttpClient.