c# - 通过 IHttpClientFactory 忽略 SSL 连接错误

标签 c# ssl asp.net-core httpclient

我在从我的 asp.net core 2.2 项目连接到 https 站点(如 there)时遇到问题.我使用 IHttpClientFactory 在 Startup.cs 中创建类型化的 HttpClient

services.AddHttpClient<ICustomService, MyCustomService>();

而且我不明白,如果不像这样手动创建 HttpClient,我怎么能忽略 SSL 连接问题

using (var customHandler = new HttpClientHandler())
{
    customHandler.ServerCertificateCustomValidationCallback  = (m, c, ch, e) => { return true; };
    using (var customClient = new HttpClient(customHandler)
    {
        // my code
    }
}

最佳答案

使用ConfigureHttpMessageHandlerBuilder:

services.AddHttpClient<ICustomService, MyCustomService>()
    .ConfigureHttpMessageHandlerBuilder(builder =>
    {
        builder.PrimaryHandler = new HttpClientHandler
        {
            ServerCertificateCustomValidationCallback = (m, c, ch, e) => true
        };
    });

关于c# - 通过 IHttpClientFactory 忽略 SSL 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283898/

相关文章:

c# - Fluent Nhibernate 将 MySQL Time(6) 映射到 C# DateTime 的问题

c# - 如果我的程序不是在管理模式下运行,我该如何阻止它运行? XP 和 Windows 7

node.js - 谷歌云 Node.js 套接字服务器应用程序托管

c# - 通过 SSL 使用 HttpWebRequest 发送请求时出现错误 502(错误的网关)

c# - Telerik Net Core Treeview 模板-从所选项目的输入框中获取值

C# DirectorySearcher 过滤器

android - 如何在 Android 中安装 Charles Proxy ssl 证书

c# - .NET Core (NET Standard 2.1) 中 DataTable/DataSet 的替换

docker - mcr.microsoft.com/dotnet/core/aspnet :2. 2 对于 Windows 容器不可用

c# - IHTMLInputElement 和 HTMLInputElement 之间的区别