2012-10-29 238 views
1


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

我认为我在Linux上运行它的方式可能是错误的。其实我使用VS2012进行本地开发,当我想把它放在服务器上时,我使用mono server.exe上传VS和Im编译的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 

      } 

     } 
    } 

回答

5

好吧,我研究有关的InnerException深一点,在这里找到了解决办法:

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

的荣誉属于paksys从MSDN

的问题是在客户端和我改变

Stream.AuthenticateAsClient(hostname); 

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

为什么同样的方法不为我工作?即使上面提到的变化发生,我也会得到同样的错误。 –

+0

有人帮我吗? –

+0

嗨。我也有同样的问题。即使你改变了这个问题也没有修复。不知道有更多的信息? – virtouso

1

我只是有相同问题和接受的答案不适合我。问题在于服务器证书使用SHA-2。下面是我用它来修复它的代码:

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

希望它可以帮助别人

相关问题