如何使用ConfigurePrimaryHttpMessageHandler泛型 [英] How to use ConfigurePrimaryHttpMessageHandler generic

查看:744
本文介绍了如何使用ConfigurePrimaryHttpMessageHandler泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Typed HttpClient添加一个HttClientHandler以便包括证书身份验证。

I want to add an HttClientHandler for a Typed HttpClient in order to include certificate authentication.

我在互联网上找到的所有示例都像这样:

All the examples I'm finding on the internet are like this:

services.AddHttpClient<IMyService, MyService>()
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        return new HttpClientHandler()
        {
            // Set here whatever you need to get configured
        };
    });

但是我不想在这里包含获取证书的所有逻辑,所以我想使用 ConfigurePrimaryHttpMessageHandler<> 的通用版本并编写我自己的消息处理程序以将证书包括在请求中。

But I don't want to include all the logic to obtain the certificate here, so I would like to use the generic version of ConfigurePrimaryHttpMessageHandler<> and write my own message handler to include the certificate in the request.

问题是我正在努力了解如何实现消息处理程序……我应该从 HttpClientHandler 继承吗?

The problem is that I'm struggling to understand how should I implement the message handler... should I inherit from HttpClientHandler ??

请帮助!

更新

我最初怀疑并@Nkosi确认是从HttpClient派生的处理程序是这种情况下的处理方式。最后的代码类似于:

As I initially suspected, and @Nkosi confirmed, to derive from HttpClient handler is the way to go in this scenario. The code in the ends looks similar to this:

public class MyHttpClientHandler : HttpClientHandler
{
    private readonly IMyConfiguration _myConfiguration;

    public MyHttpClientHandler(IMyConfiguration myConfiguration)
    {
        _myConfiguration = myConfiguration;

        using (var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine))
        {
            certStore.Open(OpenFlags.ReadOnly);
            var certCollection = certStore.Certificates.Find(
                X509FindType.FindBySerialNumber,
                _myConfiguration.MyCertificateSerial,
                true);

            X509Certificate2 certificate = certCollection[0];    
            ClientCertificateOptions = ClientCertificateOption.Manual;
            SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
            ClientCertificates.Add(certificate);
        }
    }
}

非常重要!

另一方面,在尝试注册我的http客户端处理程序时,我注意到它从未被调用过。经过一番谷歌搜索后,我发现当前存在一个有关此问题的公开错误( https://github.com / aspnet / Extensions / issues / 851 )。因此,在解决问题之前,您需要按以下方式配置处理程序:

On the other hand while trying to register my http client handler I noticed it was never being called. After some googling I found out that currently there's an open bug about that (https://github.com/aspnet/Extensions/issues/851). So until it get's fixed you need configure your handler this way:

services.AddTransient<MyHttpClientHandler>();

services.AddHttpClient<IMyService, MyService>()
    .ConfigurePrimaryHttpMessageHandler(sp => sp.GetRequiredService<MyHttpClientHandler>());


推荐答案

HttpClientHandler派生或任何 HttpMessageHandler 派生类。

public class MyHttpClientHandler : HttpClientHandler {

    public MyHttpClientHandler() {
        //Set here whatever you need to get configured
    }

    //...override members as needed
}

使用适当的扩展名呼叫您的处理程序

Call your handler using appropriate extension

services
    .AddHttpClient<IMyService, MyService>()
    .ConfigurePrimaryHttpMessageHandler<MyHttpClientHandler>();

MyHttpClientHandler 将通过作用域解析共享正在构造的处理程序的生命周期的服务提供商。

The MyHttpClientHandler will be resolved from a scoped service provider that shares the lifetime of the handler being constructed.

这篇关于如何使用ConfigurePrimaryHttpMessageHandler泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆