简体   繁体   English

如何注册将验证域用户的 HttpClient

[英]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.

相关问题 如何在 dotnetcore 中对不属于域用户的用户进行身份验证? - How can I authenticate a user that does not belong to Domain Users in dotnetcore? 如何验证与外部提供商 (Google) 的 HttpClient 连接 - How to authenticate an HttpClient connection with an external provider (Google) 如何验证域? - How can I authenticate to a domain? C# 验证 LDAP 域下的用户 - C# authenticate user under LDAP Domain 如何使用 autofac 注册类型化的 httpClient 服务? - How to register typed httpClient service with autofac? 如何在 Blazor 客户端中注册 HttpClient 处理程序? - How to register HttpClient Handler in Blazor Client? 连接到域后无法访问C#,对域用户进行身份验证 - Authenticate domain user after connection to domain is inaccessible C# 在子域上对用户进行身份验证并保持在父域上登录? - Authenticate user on Sub-domain and remain logged in on parent domain? 如何使用来自WebAPI的域的用户对WebAPI上不同计算机上的客户端进行身份验证? - how can authenticate a client on a different machine on a WebAPI with a user from WebAPI's domain? 注册并使用用户的电子邮件域 - Register and use User's email domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM