gpt4 book ai didi

c# - SslStream:处理证书时发生未知错误

转载 作者:行者123 更新时间:2023-12-04 12:04:05 25 4
gpt4 key购买 nike

我正在尝试使用 SslStream 和 TLS 1.2 协议(protocol)与远程服务器建立 TCP 连接。代码如下:

_tcpClient.Connect(endPoint);

var certificate = new X509Certificate2(_settings.CertificateFilePath, _settings.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
var certificates = new X509CertificateCollection { certificate };

_nStream = _tcpClient.GetStream();
_sslStream = new SslStream(_nStream, false,
(o, x509Certificate, chain, errors) => true,
(o, s, collection, x509Certificate, issuers) =>
{ return collection[0]; }
);

_sslStream.AuthenticateAsClient(_settings.HostIpAddress, certificates, SslProtocols.Tls12, true);
_sslStream.Write(someData, 0, someData.Length);

但是,我遇到了一个异常(exception):

System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: An unknown error occurred while processing the certificate

--- End of inner exception stack trace

at System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) at System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)



我启用了 SChannel 日志记录并在 Windows 事件日志中发现了这一点:

The remote server has requested SSL client authentication, but no suitable client certificate could be found. An anonymous connection will be attempted.



然后我按照 here 的描述启用了 System.Net 日志记录。并得到 this log (我从中删除了一些证书数据)。看起来客户端证书没问题,但由于某种原因日志显示 Remote certificate: null尽管显然有一些从远程服务器发回的数据看起来很像证书。最后,日志显示 returned code=CertUnknown .我不知道问题可能出在哪里(远程服务器证书?我的代码?远程/本地服务器设置?),希望有任何帮助。

笔记:
如果我通过指定 SslProtocols.Ssl3 更改我的代码以使用 SSL 3而不是 SslProtocols.Tls12一切正常。但我真的需要使用 TLS,因为这是远程服务器所有者要求做的。

最佳答案

我们让我们的 websockets 服务器使用 TLS 1.2。
手动添加“ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;”解决了这个问题。

关于c# - SslStream:处理证书时发生未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33341149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com