【问题标题】:SslStream TcpClient - Received an unexpected EOF or 0 bytes from the transport streamSslStream TcpClient - 从传输流接收到意外的 EOF 或 0 字节
【发布时间】:2013-04-04 16:23:04
【问题描述】:

我正在尝试通过 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;
}

【问题讨论】:

    标签: c#-4.0 tcp sslstream


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      这发生在我更新到 Windows build 20H2 后。它曾经只与:

      ssl.AuthenticateAsClient(_host);
      

      但在将enabledSslProtocols 参数显式设置为SslProtocols.Default 后,我设法在不使用证书的情况下再次连接:

      ssl.AuthenticateAsClient(_host, null, SslProtocols.Default, false);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-27
        • 2022-12-16
        • 1970-01-01
        • 2016-06-07
        • 1970-01-01
        相关资源
        最近更新 更多