首页 > 解决方案 > 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 通用的单一密码:

点网密码套件

我花了很多时间来理解错误来自这里......如果它真的来自这里。

因此,该分析产生了两个问题:

备注:即使是 Fiddler 也有这个问题!这是可以理解的,因为它也依赖于点网框架技术: 提琴手握手

标签: c#ssl.net-corehttpswebclient

解决方案


我遇到了同样的问题,我的自动测试(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;
}

推荐阅读