[英]Adding Cipher suite to TLS1.2 of HttpClient of dotnetcore 3.1
连接西数网站时遇到如下异常:
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# 编写以符合本网站的要求? 我已经看到互联网上的讨论规定,也许不允许美国公司即微软公司出口强密码算法......如果这是真的,那么 firefox (美国也是)使用与西方数字(美国也是)相同的套件怎么样。
是否有可能在 C# 中使用另一个库(我考虑开放 SSl)但另一个库确实提供了 https 的所有层(即建议等效于 httpClient)/跨平台如何避免失去 dotnetcore 的跨平台功能...... .
.NET Core 使用原生 TLS 堆栈支持的密码,即 SChannel。 支持哪些密码取决于 Windows 的版本。 您的操作系统支持哪些密码(记录在 Windows 7 的 TLS 密码套件中。如您所见,您的操作系统不支持服务器提供的密码。
使用 Firefox 或 Chrome 浏览器情况有所不同。 它们带有自己的堆栈,因此不受操作系统提供的限制。 这就是他们工作的原因。
我遇到了同样的问题,我的自动测试(dotnetcore3.1)在 WS 2012 R2 机器上运行,我必须调用第三方 API,它只接受两个密码套件:TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (0xc02f) (AES_25603GCM_RSA.38_4)
C# HttpClient 依赖于主机系统中的密码套件,而 Chrome、Firefox 和 Curl 则拥有自己的安全和加密系统。 WS 2012 R2 没有这两个密码,我不知道如何将它们添加到机器中,这些密码没有 windows 更新。
我选择使用非常酷的 NuGet 数据包CurlThin作为解决方案。 使用它,我们可以为请求设置自己的密码套件,因此我们不需要对服务器端做任何事情。
我已经安装了两个数据包: CurlThin本身和CurlThin.Native 。
使用 header 到 HTTPS 端点的 GET 请求的生成代码如下所示:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.