[英]How to register HttpClient that will authenticate domain user
I have to configure HttpClient to automatically pass Windows credentials (company API that I'm calling is using Windows Authentication).我必须将 HttpClient 配置为自动传递 Windows 凭据(我正在调用的公司 API 正在使用 Windows 身份验证)。 So far I was creating HttpClient inside my methods like so:
到目前为止,我在我的方法中创建了 HttpClient,如下所示:
var credentialsCache = new CredentialCache {{
new Uri("https://localhost"),
"NTLM",
CredentialCache.DefaultNetworkCredentials
}};
var handler = new HttpClientHandler { Credentials = credentialsCache };
var client = new HttpClient(handler);
but since this could lead to socket exhaustion I want to use factory.但由于这可能导致套接字耗尽,我想使用工厂。 Therefore inside
Startup.cs
I would add HttpClient like so:因此在
Startup.cs
我会像这样添加 HttpClient :
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddHttpClient();
}
Then I would inject factory into my service via constructor:然后我会通过构造函数将工厂注入到我的服务中:
public class MyService
{
private readonly IHttpClientFactory clientFactory;
public MyService(IHttpClientFactory clientFactory)
{
this.clientFactory = clientFactory;
}
public async Task MakeHttpRequest()
{
var request = new HttpRequestMessage(HttpMethod.Get, "https://localhost/endpoint");
var client = clientFactory.CreateClient();
var response = await client.SendAsync(request);
// ...
}
}
How can I configure client created this way to use network credentials?如何配置以这种方式创建的客户端以使用网络凭据? Ideally I would like to configure this only once, inside
ConfigureServices()
理想情况下,我只想在
ConfigureServices()
中配置一次
You may look at named clients and use ConfigurePrimaryHttpMessageHandler
method then您可以查看命名客户端并使用
ConfigurePrimaryHttpMessageHandler
方法
services
.AddHttpClient("myService")
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
Credentials = credentialsCache
});
HttpClient
in MyService
should be created using the given name MyService
中的HttpClient
应该使用给定的名称创建
...
var client = clientFactory.CreateClient("myService");
...
You can use the options pattern to set configuration for how IHttpClientFactory
creates the HttpClient
.您可以使用选项模式来设置
IHttpClientFactory
如何创建HttpClient
的配置。 You can configure these options in the following ways您可以通过以下方式配置这些选项
Un-named options, these will apply to all instances of HttpClient
that are created by the factory未命名的选项,这些将应用于工厂创建的所有
HttpClient
实例
services.Configure<HttpClientFactoryOptions>(options =>
{
options.HttpMessageHandlerBuilderActions.Add(builder =>
{
var credentialsCache = new CredentialCache {{
new Uri("https://localhost"),
"NTLM",
CredentialCache.DefaultNetworkCredentials
}};
builder.PrimaryHandler = new HttpClientHandler { Credentials = credentialsCache };
});
});
Named options, these only apply to instances of HttpClient
where you pass the name to the CreateClient
method on the factory命名选项,这些仅适用于您将名称传递给工厂上的
CreateClient
方法的HttpClient
实例
services.Configure<HttpClientFactoryOptions>("myclient", options =>
{
options.HttpMessageHandlerBuilderActions.Add(builder =>
{
var credentialsCache = new CredentialCache {{
new Uri("https://localhost"),
"NTLM",
CredentialCache.DefaultNetworkCredentials
}};
builder.PrimaryHandler = new HttpClientHandler { Credentials = credentialsCache };
});
});
To use the named options you would do要使用命名选项,你会做
var client = clientFactory.CreateClient("myclient");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.