c# - dotnetcore 3.1 的 HttpClient 的 TLS1.2 添加密码套件
问题描述
连接西数网站时遇到如下异常:
22:02:34,803 | HttpGrabber | DEBUG | Grabbing: GET https://shop.westerndigital.com/de-de/products/internal-drives/wd-red-sata-2-5-ssd#WDS200T1R0A
22:02:34,858 | HttpGrabber | DEBUG | System.Net.Http.SocketsHttpHandler.Http2Support: True
22:02:34,865 | HttpGrabber | DEBUG | System.Net.Http.UseSocketsHttpHandler: True
22:02:35,067 | HttpGrabber | ERROR | System.AggregateException: One or more errors occurred. (The SSL connection could not be established, see inner exception.)
---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> System.ComponentModel.Win32Exception (0x80090326): Le message reçu était inattendu ou formaté de façon incorrecte.
--- End of inner exception stack trace ---
我认为 C# 代码是正确的,因为我有 3/4 单元测试通过:
[TestCase("https://allianz-fonds.webfg.net/sheet/fund/FR0013192572/730?date_entree=2018-04-04")]
[TestCase("https://www.galaxus.de/de/s1/product/zotac-zbox-magnus-en72070v-intel-core-i7-9750h-0gb-pc-13590721")]
[TestCase("https://www.hystou.com/Gaming-Mini-PC-F7-with-Nvidia-GeForce-GTX-1650-p177717.html")]
[TestCase("https://shop.westerndigital.com/de-de/products/internal-drives/wd-red-sata-2-5-ssd#WDS200T1R0A")]
ssllabs完成的 SSL 诊断给出了西数网站处理的受支持密码套件列表:
Firefox 成功连接到该网站,Wireshark 发现 Firefox 在列表中有 1 个密码:
但是,我的 dotnet 核心应用程序在 ssl 握手中具有致命性,因为它没有与 WD 通用的单一密码:
我花了很多时间来理解错误来自这里......如果它真的来自这里。
因此,该分析产生了两个问题:
是否可以在我的 dot net core 3.1 应用程序中添加一个密码套件,用 C# 编写以符合该网站的要求?我已经看到互联网上的讨论规定,也许不允许美国公司即微软公司出口强大的加密算法......如果这是真的,那么使用与 Western Digital 相同的套件(美国也是)的 Firefox(美国也是)怎么样。
是否有可能在 C# 中使用另一个库(我考虑开放 SSl),但另一个库确实提供了所有层的 https(即建议等效于 httpClient)/跨平台如何避免失去 dotnetcore 的跨平台功能...... .
解决方案
我遇到了同样的问题,我的自动测试(dotnetcore3.1)在 WS 2012 R2 机器上运行,我必须调用仅接受两个密码套件的第三方 API:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f) TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (0xc030)。
C# HttpClient 依赖于主机系统中的密码套件,而 Chrome、Firefox 和 Curl 则拥有自己的安全和加密系统。WS 2012 R2 没有这两个密码,我不知道如何将它们添加到机器中,这些密码没有 Windows 更新。
我选择使用非常酷的 NuGet 数据包CurlThin作为解决方案。使用它,我们可以为请求设置自己的密码套件,因此我们不需要对服务器端做任何事情。
我已经安装了两个数据包:CurlThin本身和CurlThin.Native。
带有标头的 GET 请求到 HTTPS 端点的结果代码如下所示:
using CurlThin;
using CurlThin.Enums;
using CurlThin.Helpers;
using CurlThin.Native;
using CurlThin.SafeHandles;
using System.Text;
private static string GetToken()
{
//This string is for extracting libcurl and ssl libs to the bin directory.
CurlResources.Init();
var global = CurlNative.Init();
var easy = CurlNative.Easy.Init();
string content;
try
{
var dataCopier = new DataCallbackCopier();
CurlNative.Easy.SetOpt(easy, CURLoption.URL, "https://someendpoints.net/thatendpoint?fake=true");
CurlNative.Easy.SetOpt(easy, CURLoption.WRITEFUNCTION, dataCopier.DataHandler);
//This string is needed when you call a https endpoint.
CurlNative.Easy.SetOpt(easy, CURLoption.CAINFO, CurlResources.CaBundlePath);
var headers = CurlNative.Slist.Append(SafeSlistHandle.Null, "Authorization: Bearer blablabla");
CurlNative.Easy.SetOpt(easy, CURLoption.HTTPHEADER, headers.DangerousGetHandle());
//Your set of ciphers, full list is here https://curl.se/docs/ssl-ciphers.html
CurlNative.Easy.SetOpt(easy, CURLoption.SSL_CIPHER_LIST, "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256");
CurlNative.Easy.Perform(easy);
content = Encoding.UTF8.GetString(dataCopier.Stream.ToArray());
}
finally
{
easy.Dispose();
if (global == CURLcode.OK)
CurlNative.Cleanup();
}
return content;
}
推荐阅读
- apache - Phabricator 通知服务器 SSL 错误
- c# - VisualStudio 2019 未显示与 VisualStudio 2017 相同的 NET 框架
- sql - 函数不支持排序规则
- c++ - 在与 Odeint 结合的类中使用 Eigen Matrix Ref
- python - Python 对象如何变成原始类型
- git - 使用 Github 和 Gitlab
- swift - 从那里使用推送通知管理器需要重定向到特定的视图控制器
- javascript - 通过 eval 语句创建的 ES6 类的可见性
- javascript - jQuery 直到双击才运行 JSON API 函数
- amazon-s3 - 如何限制对 AWS S3 的请求?