c# - .NET Core - 禁用所有 SSL 验证

标签 c# asp.net-core ssl

我正在开发一个 .NET Core 应用程序,其中对第 3 方 SSL 证书(通过 VPN 发生)的验证失败(服务器证书未使用根 CA 正确签名,因此无法使用 openssl 进行验证,我正在使用)。
禁用HttpClient 的验证很容易。我手动创建。像这样的东西,如 bypass invalid SSL certificate in .net core 中所述:

using (var httpClientHandler = new HttpClientHandler())
{
   httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
   using (var client = new HttpClient(httpClientHandler))
   {
       // Make your request...
   }
}
不过,在我的情况下,我使用第 3 方 DLL 进行连接,因此隐藏了它如何执行请求的内部结构,并且我无权禁用连接的 SSL 验证。 .NET Core 3.1 中有没有一种方法可以完全禁用来自应用程序的任何请求的 SSL 验证?

最佳答案

IMPORTANT - this is a hacky workaround that circumvents a basic principle of SSL connection i.e. verification of the certificate origin. It's only useful if you have a situation where a self-signed certificate used on a server does not allow correct verification for some reason. When following standard practice, the described workaround should only ever be done in development or test scenarios.


我发现服务器使用的不是完全充当 CA 的自签名证书。 openssl在建立 SSL 握手(比 Windows 更严格地执行 SSL 标准)时,正在客户端 Linux 服务器上使用,这需要为 x509 证书正确签名的 CA 链。这在这种情况下不存在,因此在客户端的 Linux 服务器上不能以 root 身份信任证书,这就是验证不起作用的原因。
我发现有迹象表明在 .NET Core 中没有禁用 SSL 验证(或严格来说是“验证”)步骤的全局选项。
相反,在客户端的 Linux 主机上,我使用 stunnel 将 tcp 连接代理到 SSL 连接。在应用程序的 kubernetes pod 中运行,并通过在 stunnel 配置中关闭链验证来关闭 SSL 验证,例如:
[destination.FQDN.com]
client = yes
accept = 127.0.0.1:1234
connect = $remote:1234
verifyChain = no
然后我从 appsettings.json 连接到 tcp:127.0.0.1:1234 (即 stunnel 的本地接收端)。 stunnel然后成功建立到ssl:127.0.0.1:1234的加密 channel .

关于c# - .NET Core - 禁用所有 SSL 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65784151/

相关文章:

c# - 哪个更正确,为什么?

c# - linq select 语句与直接访问

c# - 我可以查明字符串到 ANSI 的转换是否会导致数据丢失吗?

amazon-web-services - AWS Codebuild .NET Core构建Docker镜像

http - 如何在 HTTPS 中包装外部 HTTP 数据源?

java - 信任 java Websocket 客户端中的所有证书

c# - Global.asax 中的自动事件连接

c# - 为什么异常重定向到不同的页面?

asp.net-mvc - 在 nginx 反向代理后面设置 ASP NET Core MVC 应用程序的虚拟路径

ssl - JSSEHelper 不提供正确的 SSLSocketFactory 用于在 Websphere 8.0 中建立安全连接