首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSLStream:“调用SSPI失败”异常

SSLStream:“调用SSPI失败”异常
EN

Stack Overflow用户
提问于 2012-10-29 20:55:33
回答 2查看 31.5K关注 0票数 3

我有一个奇怪的问题:我用c#编写了一个基于.net2的服务器和客户端,它们通过.net 2 SslStream聊天。在c1和s之间有一个用于发送命令的连接,在Cl和s之间有一个用于发送文件的连接。

在本地,一切都在我的windows机器上运行得很好。但是如果我在我的Linux服务器上运行服务器(使用mono),共享文件在某些情况下不起作用。我对文件有不同的分类,在第三种情况下,服务器在SSLStream.AuthenticateAsServer(....);上挂起,而客户端抛出一个异常,消息为"A Call to SSPI Failed,see inner“。内部异常是一个System.ComponentModel.Win32Exception,消息为“收到的消息是意外的或格式错误的”。

我认为我在linux上运行它的方式可能是错误的。实际上,我正在使用VS2012进行本地开发,当我想将它放到服务器上时,我会使用mono server.exe上传VS和Im编译后的可执行文件来启动它。但我也尝试过在Windows上使用monodevelop来编译它( monodevelop编译出来的东西也可以在本地运行),并在linux服务器上使用它,但结果是一样的。

任何帮助都将不胜感激。

以下是我的简短代码:

客户端:

代码语言:javascript
复制
//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

}

服务器:

代码语言:javascript
复制
 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

            }

        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-29 22:55:53

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

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

功劳来自MSDNpaksys

问题出在客户端,我改变了

代码语言:javascript
复制
Stream.AuthenticateAsClient(hostname);

代码语言:javascript
复制
X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls, false);
票数 7
EN

Stack Overflow用户

发布于 2017-03-09 01:19:21

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

代码语言:javascript
复制
X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls12, false);

  • 请注意,与接受答案的唯一区别是sslProtocol

希望它能帮助一些人

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13122093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档