首页 > 解决方案 > 使用带有自定义 HttpClientHandler 和硬件令牌的 HttpClient 连接到 API 的问题

问题描述

我目前正在开发一个需要定期向 API 发送请求并尽可能长时间保持活动状态的客户端。

我们在物理令牌上使用证书并检查它是否正常工作,我们签署一些数据并尝试建立与 API 的新连接。

实际上有几个工作人员并行发送不同类型的请求,每个工作人员都托管在不同的 ASP.NET Core 后台服务上:

第三个目前每次检查之间的间隔约为 150 秒,并且在每次迭代中,我们使用自定义的 HttpClientHandler 创建一个新的 HttpClient。在 HttpClientHandler 中,我们设置了一个自定义验证回调(我们在一个测试平台上,现在我们只返回 true)、证书和 ssl 协议(我们尝试了任何组合)

在客户端正常工作一段时间后(它持续了 20 分钟到最长约 20 小时),它开始给出一些错误,如下所示:

System.Net.Http.HttpRequestException: An error occurred while sending the request.
 ---> System.IO.IOException: The read operation failed, see inner exception.
 ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
 ---> System.ComponentModel.Win32Exception (0x80090304): Impossible to contact local security authority   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm)
   at System.Net.Security.SslStream.ReplyOnReAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Byte[] buffer)
   at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory`1 buffer)
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.ReadAsyncInternal[TIOAdapter](TIOAdapter adapter, Memory`1 buffer)
   at System.Net.Http.HttpConnection.FillAsync(Boolean async)
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean async, Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.SendAsyncCore(HttpRequestMessage request, HttpCompletionOption completionOption, Boolean async, Boolean emitTelemetryStartStop, CancellationToken cancellationToken)

在互联网上检查时,此错误不会导致任何结果,希望这里有一些 .NET 专家可以帮助我们。

我们开始使用带有命名客户端的 HttpClientFactory,但它只会导致更少的错误,我们尝试切换到效率更低的方法,结果只会变得更糟。

有 4 件有趣/令人沮丧的事情:

任何帮助和/或建议将不胜感激

编辑:添加了两个代码示例,我在其中创建客户端并发送请求,并没有做任何特别的事情

if (!_loggedIn) await Login();
        
var httpRequest = new HttpRequestMessage
{
    Content = new StringContent(request.XmlContent, Encoding.UTF8, "text/xml"),
    Method = HttpMethod.Post,
        RequestUri = new Uri(_baseUrl)
    };

    return await SendAsync(httpRequest);

services.AddTransient<CustomHttpClient>(provider =>
{
    var handler = new HttpClientHandler
    {
        SslProtocols = SslProtocols.None,
        AllowAutoRedirect = false,
        MaxConnectionsPerServer = 5,
        ClientCertificates = {new AuthCertificateContext(issuer, pin).GetCertificate()},
        ServerCertificateCustomValidationCallback = (_, _, _, _) => true
    };
                    
    return new CustomHttpClient(new HttpClient(handler, true), number);
});

标签: c#asp.net.netsmartcard

解决方案


推荐阅读