如何使用 ConfigurePrimaryHttpMessageHandler 泛型 [英] How to use ConfigurePrimaryHttpMessageHandler generic
问题描述
我想为 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
派生类.
Derive from HttpClientHandler
or any HttpMessageHandler
derived class.
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屋!