【问题标题】:SSLStream: "A Call to SSPI Failed" ExceptionSSLStream:“对 SSPI 的调用失败”异常
【发布时间】:2012-10-18 19:04:42
【问题描述】:


我有一个奇怪的问题:我在基于 .net2 的 c# 中编写了一个服务器和客户端,它们通过 .net 2 SslStream 聊天。 Cl和S之间有1个发送命令的连接,cl和s之间有1个发送文件的连接。
本地一切都在我的Windows机器上运行良好。但是,如果我在我的 Linux 服务器上运行服务器(使用单声道),则在某些情况下共享文件不起作用。我有不同的文件类别,在 3 个中的 2 个中,它的工作,在第三个中,服务器挂在SSLStream.AuthenticateAsServer(....); 上,客户端抛出一个异常,并显示消息“对 SSPI 的调用失败,请参阅内部异常”。 内部异常是 System.ComponentModel.Win32Exception,带有消息“收到的消息是意外的或格式错误。”

我认为我在 linux 上运行它的方式可能是错误的。实际上我使用 VS2012 进行本地开发,当我想把它放在服务器上时,我通过 VS 上传编译的 exe,然后我使用mono server.exe 启动它。但是我也尝试在 Windows 上使用 monodevelop 来编译它(从 monodevelop 编译的东西也可以在本地工作)并在 linux 服务器上使用它,但结果相同。

任何帮助将不胜感激。



这是我的缩短代码:

客户:

//gets auth guid before, then it does
Thread Thre = new Thread(sendfile);
Thre.Start(authguid);


private void sendfile(string guid){
TcpClient cl = new TcpClient();
cl.Connect(hostname, 8789);
SslStream Stream = new SslStream(cl.GetStream(), true, new RemoteCertificateValidationCallback(ServerConnector.ValidateServerCertificate));
Stream.AuthenticateAsClient(hostname);//throws the exception
//sends auth guid and file

}

服务器:

 public class FServer
    {

        protected static bool halt = false;
        protected List<FConnection> connections;
        private TcpListener tcpServer;
        private IPEndPoint ipEnde = new IPEndPoint(IPAddress.Any, 8789);
        private delegate void dlgAccept();
        private dlgAccept accepting;
        private IAsyncResult resAccept;

        public FServer()
        {
            tcpServer = new TcpListener(ipEnde);
            connections = new List<FConnection>();
            tcpServer.Start();
            accepting = new dlgAccept(accept);
            resAccept = accepting.BeginInvoke(null, null);


        }

        public void accept()
        {
            do
            {
                if (tcpServer.Pending())
                    connections.Add(new FConnection(tcpServer.AcceptTcpClient(), this));

                else
                    Thread.Sleep(750);
            } while (!halt && tcpServer != null);
        }

        public class FConnection 
        {
           public SslStream stream;
            //.....some other properties

           public static bool ValidateClientCertificate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            {
                return true;

            }

            public FConnection(TcpClient cl, FServer inst)
            {
                instance = inst;
                client = cl;

                    stream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateClientCertificate), null);
                    stream.AuthenticateAsServer(instance.mainserver.serverCert, false, SslProtocols.Tls, false);//hangs sometimes on this
                    if (stream.IsAuthenticated && stream.IsEncrypted)
                    {
                    }
                    else
                    {
                        this.Dispose();
                    }

                    //auth and receiving file

            }

        }
    }

【问题讨论】:

    标签: c# mono .net-2.0 sslstream sspi


    【解决方案1】:

    我只是遇到了同样的问题,接受的答案对我不起作用。问题是服务器证书使用的是 SHA-2。这是我用来修复它的代码:

    X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
    Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls12, false);
    
    • 请注意,与已接受答案的唯一区别是 sslProtocol

    希望对大家有所帮助

    【讨论】:

      【解决方案2】:

      好的,我对内部异常进行了更深入的研究,并在这里找到了解决方案:

      http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/18b4a0ce-b348-403d-8655-fe9d558f8d6b

      感谢来自 MSDN 的 paksys

      问题出在客户端,我改了

      Stream.AuthenticateAsClient(hostname);
      

      X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
      Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls, false);
      

      【讨论】:

      • 为什么同样的方法对我不起作用?即使上面提到了所做的更改,我也会遇到同样的错误。
      • 有人帮帮我吗?
      • 嗨。我也有同样的问题。即使你改变了问题也没有解决。不知道有更多的信息?
      • @virtouso 你得到的内部异常是什么?
      • 内部异常是:clients客户端和服务端无法通信,因为它们没有共同的算法。
      猜你喜欢
      • 2016-04-10
      • 2021-06-25
      • 1970-01-01
      • 2014-02-15
      • 2022-12-18
      • 2019-07-05
      • 2015-07-16
      • 2016-10-21
      • 1970-01-01
      相关资源
      最近更新 更多