我有一个奇怪的问题:我用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服务器上使用它,但结果是一样的。
任何帮助都将不胜感激。
以下是我的简短代码:
客户端:
//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
}
}
}发布于 2012-10-29 22:55:53
好吧,我对内部异常进行了更深入的研究,并在这里找到了解决方案:
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);发布于 2017-03-09 01:19:21
我只是遇到了同样的问题,而且公认的答案对我不起作用。问题是服务器证书使用的是SHA-2。下面是我用来修复它的代码:
X509Certificates.X509Certificate2Collection xc = new X509Certificates.X509Certificate2Collection();
Stream.AuthenticateAsClient(hostname, xc, Security.Authentication.SslProtocols.Tls12, false);希望它能帮助一些人
https://stackoverflow.com/questions/13122093
复制相似问题