使用 MSAL.NET 提供你自己的 HttpClient 和代理Providing your own HttpClient and proxy using MSAL.NET
初始化公共客户端应用程序时,可以使用 .WithHttpClientFactory method
提供你自己的 HttpClient。When initializing a public client application, you can use the .WithHttpClientFactory method
to provide your own HttpClient. 提供你自己的 HttpClient 可以实现高级方案,例如对 HTTP 代理的精细控制、自定义用户代理标头,或者强制 MSAL 使用特定的 HttpClient(例如在 ASP.NET Core Web 应用/API 中)。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).
使用 HttpClientFactory 进行初始化Initialize with HttpClientFactory
下面的示例展示了如何创建 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 和 Xamarin iOSHttpClient and Xamarin iOS
使用 Xamarin iOS 时,建议创建一个 HttpClient
并让其针对 iOS 7 和更高版本显式使用基于 NSURLSession
的处理程序。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 会自动创建一个 HttpClient
,它针对 iOS 7 和更高版本使用 NSURLSessionHandler
。MSAL.NET automatically creates an HttpClient
that uses NSURLSessionHandler
for iOS 7 and newer. 有关详细信息,请阅读适用于 HttpClient 的 Xamarin iOS 文档。For more information, read the Xamarin iOS documentation for HttpClient.