SslStream TcpClient - 从传输流中收到意外的EOF或0字节

时间:2013-04-04 16:23:04

标签: c#-4.0 tcp sslstream

我正在尝试通过SslStream / Tcp客户端连接到服务器。每次我这样做时都会收到异常声明:在AuthenticateAsClient行中从传输流收到意外的EOF或0字节。

我启用了跟踪日志记录,并在日志中收到以下两个错误:

System.Net信息:0:[12260] SecureChannel#66702757 ::。ctor(hostname = xxx.xx.xx.xxx,#clientCertificates = 0,encryptionPolicy = RequireEncryption)

System.Net信息:0:[12260] SecureChannel#66702757 - 剩下0个客户端证书可供选择。

有人能就如何解决这个问题给我任何建议吗?不确定为什么,但如果这有任何区别,它会向外部捕获。

try
{
    TcpClient client = new TcpClient(_host, _port);
    // _stream = client.GetStream();

    _stream = new SslStream(client.GetStream(), false, ValidateServerCertificate, null);
    _sslReader = new StreamReader(client.GetStream());

    try
    {
        _stream.AuthenticateAsClient(_host);
    }
    catch (AuthenticationException e)
    {
        client.Close();
        return;
    }

    HandleConnect();
}
catch (Exception e)
{
    _logger.Error(e.BuildExceptionInfo());
}

public bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    if (sslPolicyErrors == SslPolicyErrors.None || sslPolicyErrors == SslPolicyErrors.RemoteCertificateNameMismatch)
        return true;

    return false;
}

1 个答案:

答案 0 :(得分:2)

我的猜测是服务器需要客户端证书。