C# 移动到.NET 4.51时SSLStream引发异常
以前在.NET 3.5中,此代码运行良好:C# 移动到.NET 4.51时SSLStream引发异常,c#,.net,ssl,C#,.net,Ssl,以前在.NET 3.5中,此代码运行良好: protected bool OpenSsl(string hostSslServer) { if ((this._enableSSL == false) || (this._isSSLOpen == true)) return (true); this._isSSLOpen = false; this._sslConnectionStream = new SslStr
protected bool OpenSsl(string hostSslServer)
{
if ((this._enableSSL == false) || (this._isSSLOpen == true))
return (true);
this._isSSLOpen = false;
this._sslConnectionStream = new SslStream(this._tcpSocket.GetStream(), false, new RemoteCertificateValidationCallback(this.ValidateServerCertificate), null);
this._sslConnectionStream.AuthenticateAsClient(hostSslServer);
this._sslProtocol = _sslConnectionStream.SslProtocol.ToString();
this._isSSLOpen = true;
return (true);
}
行this.\u sslProtocol=\u sslConnectionStream.sslProtocol.toString();旨在确定正在使用的协议。但是从那以后,我发现TLS1.1和TLS1.2不是3.5中SslProtocols枚举的一部分,所以我将项目切换到了4.5。但是现在我抛出了异常,特别是在客户端行。异常为“调用SSPI失败,请参阅内部异常”。内部异常为“收到的消息意外或格式错误”
从那时起,我做了一些研究,发现我可以使用以下代码进行连接:
System.Security.Cryptography.X509Certificates.X509Certificate2Collection xc = new System.Security.Cryptography.X509Certificates.X509Certificate2Collection();
this._sslConnectionStream.AuthenticateAsClient(hostSslServer, xc, System.Security.Authentication.SslProtocols.Tls12, false);
用对客户端的调用替换上一行。然而,这里的问题是,使用这段代码(我必须承认我不理解),我将ssl协议设置为参数之一。我不想那样做。我想知道我连接的主机使用的是什么ssl协议
这有什么办法吗
此后,我将sslprotocols的参数更改为:
SslProtocols.Ssl2 | SslProtocols.Ssl3 | SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12
我还遇到了另一个异常:“客户端和服务器无法通信,因为它们没有通用的算法”我从协议列表中删除了SSL2,它可以工作。SslProtocols是一个标志,您可以将它设置为
SslProtocols.Tls11 | SslProtocols.Tls12
。如果使用Default
选项的原始重载不起作用,那是因为服务器甚至不允许TLS1.0。这不是.NET中的一个bug,很可能是服务器发生了变化啊!但是现在我遇到了这样一个错误:“客户端和服务器无法通信,因为它们没有通用的算法”