繁体   English   中英

C# 无法创建 ssl/tls 安全通道 (Restsharp)

[英]C# could not create ssl/tls secure channel (Restsharp)

我目前正在尝试连接到 api 但我收到此错误: could not create ssl/tls secure channel

该请求在通过Postman 、相同的端点、相同的证书和所有内容发出时有效,但是当我通过 restsharp 发出请求时,它会因 SSL/TLS 错误而停止

我尝试使用以下代码将安全协议强制为 TLS12: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

还尝试使用ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };删除证书验证ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; 以及此代码的其他一些变体。

也尝试在客户端上禁用它,但没有成功client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

所以我目前正在尝试使用Wireshark来获取更多信息。 据我所知,这不是密码套件的问题,因为它通过了“Client Hello”和“Server Hello”,并在“Certificate Request,Server Hello Done”时停止。

线鲨 1

当我使用 Postman 发出请求时,图像上出现的错误也会出现。

当它收到带有ACKRST标志时,它会在该行停止。

线鲨 2

有谁知道它为什么不起作用?

另外值得一提的是,我之前多次使用相同的代码来调用其他 api,它们似乎工作正常。 就是这个不管我怎么尝试都做不出来的。 我无法访问服务器,因为它是第三方 api。

对此问题的任何帮助表示赞赏。

编辑:添加我用来进行 api 调用的代码以及一些被询问的更多信息

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificateReturn, chainReturn, sslPolicyErrors) => true;

var client = new RestClient(api);
client.Timeout = -1;

//Add Certificate
X509Certificate2 cert = null;
if (GCONTABANCO.SelectCERTIFICADO(IDCONTABANCO, ref cert, ref MSG) == false) { return false; }
client.ClientCertificates = new X509CertificateCollection();
client.ClientCertificates.Add(cert);
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", clientId);
request.AddParameter("client_secret", clientSecret);
request.AddParameter("scope", "extrato.read boleto-cobranca.read boleto-cobranca.write");
IRestResponse response = client.Execute(request);

我正在使用 .net 框架 4.5.2,我尝试更新到 4.7,但错误仍然存在,目前我无法更新版本,因为它是一个大系统,不能冒险破坏它

Windows 内部版本号:21H1 (10.0.19043)

所以我在他们的 IT 架构师的帮助下设法解决了这个问题,我会在这里留下答案,以防将来有人遇到同样的问题。

由于他们通过连接发送了很多证书,因此超出了允许的限制并关闭了连接,因此我必须更改限制才能连接到服务器。 在 C# 中,必须在注册表中进行更改。

为此,我必须:

  • 打开注册表。
  • Go 到:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Messaging
  • 然后添加 MessageLimitClient DWORD 值。
  • 将值设置为 65536。
  • 重新启动机器。

这样做并再次测试后,连接成功。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM