4

How should I use HttpClientFactory to return an instance of HttpClient whose uri and credentials are determined at the point of the call?

The existing code looks like this:

var httpClientHandler = new HttpClientHandler()
    {
        Credentials = new NetworkCredential(userName, password),
    };
HttpClient client = new HttpClient(httpClientHandler);
client.BaseAddress = new Uri(_appSetting.ServiceURI);
4

3 回答 3

8

启动类中的 ConfigureServices 方法

             services.AddHttpClient("github", c =>
            {
                //c.BaseAddress = new Uri("https://api.github.com/");
                c.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
                c.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory-Sample");
                
            }).ConfigurePrimaryHttpMessageHandler(() =>
            {
                return new HttpClientHandler()
                {
                    UseDefaultCredentials = true,
                    Credentials = new NetworkCredential("", ""),
                };
            });

您的控制器将如下所示

 private readonly IHttpClientFactory _httpClientFactory;

        public DataProController(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        [HttpGet]
        public async Task<ActionResult> Get()
        {            
            var client = _httpClientFactory.CreateClient("github");
            
            client.BaseAddress = new Uri("https://api.github.com/");

            string result = await client.GetStringAsync("/");
            return Ok(result);
        }

使用 httpclientfactory 时,您可能无法在运行时设置网络凭据,可能需要在启动类中设置。您可以在此处找到有关此问题的信息。 https://github.com/aspnet/HttpClientFactory/issues/71

于 2019-02-01T11:14:19.000 回答
6

您可以像这样创建身份验证委托处理程序:

public class AuthenticationHttpMessageHandler : DelegatingHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken cancellationToken)
        {
            // Get the token or other type of credentials here
            // string scheme = ... // E.g. "Bearer", "Basic" etc.
            // string credentials = ... // E.g. formatted token, user/password etc.

            request.Headers.Authorization =
                new AuthenticationHeaderValue(scheme, credentials);

            return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
        }
    }

然后将其添加到 HttpClient 构建器和 DI 容器中:

 services
     .AddTransient<AuthenticationHttpMessageHandler>()
     .AddHttpClient("MyClient")
     .AddHttpMessageHandler<AuthenticationHttpMessageHandler>();

然后使用 IHttpClientFactory 创建 HttpClient 实例。

这种方法的核心优势是您可以清楚地分离关注点。您无需接触主处理程序,无需手动管理客户端创建,您可以利用工厂及其构建器扩展方法的全部功能。身份验证处理程序自然地注入到管道中,并为每个请求添加授权。这个处理程序可以通过抽象出凭据的来源来进一步增强,并使处理程序依赖于一些 IAuthenticationProvider 抽象,这将只需要 DI 配置而不涉及 HttpClient 配置代码。

于 2020-04-24T14:35:11.183 回答
2

如果您使用 .net 依赖注入,您可以将一个类的配置添加到您的设置代码中:

services
    .AddTransient<DataLoader>()
    .AddHttpClient<DataLoader>().ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
    Credentials = new NetworkCredential(LoadUsernameFromConfig(), LoadPasswordFromSecureLocation())
});

现在添加,DI 将HttpClient使用此凭据注入到DataLoader类中:

    public class DataLoader
    {
        private readonly HttpClient httpClient;

        public DataLoader(HttpClient httpClient)
        {
            this.httpClient = httpClient;
        }

        public async Task LoadData(string tableName)
        {
            var json = await httpClient.GetStringAsync("https://protected.example.com/json");
            ...
        }
    }

(如果我没有Imran Arshad的回答,我将无法想出这段代码:谢谢!)

于 2019-06-13T09:05:00.700 回答