hol*_*hen 6 c# ssl sslstream tls1.2
我正在尝试使用 SslStream 和 TLS 1.2 协议建立到远程服务器的 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);
Run Code Online (Sandbox Code Playgroud)
但是,我遇到了一个例外:
System.Security.Authentication.AuthenticationException:对 SSPI 的调用失败,请参阅内部异常。---> System.ComponentModel.Win32Exception: 处理证书时发生未知错误
--- 内部异常堆栈跟踪结束
在 System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) 在 System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)
我启用了 SChannel 日志记录并在 Windows 事件日志中找到了这个:
远程服务器已请求 SSL 客户端身份验证,但找不到合适的客户端证书。将尝试匿名连接。
然后我按照此处所述启用 System.Net loggiing并获取此日志(我从中删除了一些证书数据)。看起来客户端证书没问题,但由于某种原因,日志显示Remote certificate: null虽然显然有一些数据从远程服务器发回,看起来非常像证书。最后,日志说returned code=CertUnknown。我不知道问题可能出在哪里(远程服务器证书?我的代码?远程/本地服务器设置?)并希望得到任何帮助。
注意:如果我通过指定SslProtocols.Ssl3而不是SslProtocols.Tls12一切正常来更改我的代码以使用 SSL 3 。但我真的需要使用 TLS,因为这是远程服务器所有者要求做的。
小智 0
我们使 Websockets 服务器使用 TLS 1.2。
手动添加“ws.SslConfiguration.EnabledSslProtocols = System.Security.Authentication.SslProtocols.Tls12;” 解决了这个问题。
| 归档时间: |
|
| 查看次数: |
7830 次 |
| 最近记录: |