c# - SslStream.AuthenticateAsClient - 发送时无法立即完成非阻塞套接字操作

标签 c# .net sockets ftp

我目前正在尝试使用 TcpClient 连接到 FTP 服务器。
为了使用 FTPS 作为协议(protocol),我通过 NetworkStream 处理一个单独的方法,该方法使用该方法创建一个 SslStream,然后调用 SslStream.AuthenticateAsClient 并抛出异常 发送时无法立即完成非阻塞套接字操作 .实际流由 TcpClient.GetStream 方法初始化。
实际上源代码来自 Biko 库,您可以在这里找到该部分:https://biko.codeplex.com/SourceControl/latest#Biko/Starksoft%20Biko/Net/Ftp/FtpBase.cs

private Stream CreateSslStream(Stream stream)
    {
        // create an SSL or TLS stream that will close the client's stream
        SslStream ssl = new SslStream(stream, true, new RemoteCertificateValidationCallback(secureStream_ValidateServerCertificate), null);

        // choose the protocol
        SslProtocols protocol = SslProtocols.None;
        switch (_securityProtocol)
        {
            case FtpSecurityProtocol.Tls1OrSsl3Explicit:
            case FtpSecurityProtocol.Tls1OrSsl3Implicit:
                protocol = SslProtocols.Default;
                break;
            case FtpSecurityProtocol.Ssl2Explicit:
            case FtpSecurityProtocol.Ssl2Implicit:
                protocol = SslProtocols.Ssl2;
                break;
            case FtpSecurityProtocol.Ssl3Explicit:
            case FtpSecurityProtocol.Ssl3Implicit:
                protocol = SslProtocols.Ssl3;
                break;
            case FtpSecurityProtocol.Tls1Explicit:
            case FtpSecurityProtocol.Tls1Implicit:
                protocol = SslProtocols.Tls;
                break;
            default:
                throw new FtpSecureConnectionException(String.Format("unexpected FtpSecurityProtocol type '{0}'", _securityProtocol.ToString()));
        }

        // note: the server name must match the name on the server certificate.
        try
        {
            // authenticate the client
            ssl.AuthenticateAsClient(_host, _clientCertificates, protocol, true);
        }
        catch (AuthenticationException authEx)
        {
            throw new FtpAuthenticationException("Secure FTP session certificate authentication failed.", authEx);
        }

        return ssl;
    }

所以,我的问题是,我怎样才能修改源代码以便不再抛出这个异常?我无法删除身份验证,但错误也应该消失。是什么导致了这个错误?

提前致谢。

最佳答案

固定的。
我刚刚更新到更新版本的库。该问题是由作者通过添加锁定关键字解决的线程问题引起的。

关于c# - SslStream.AuthenticateAsClient - 发送时无法立即完成非阻塞套接字操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28221648/

相关文章:

c - 发送和接收不一致

c# - 在其他类中引用 Windows 窗体元素

c# - 如果在类外调用方法,如何生成编译器警告?

c# - 数据没有插入到表中?

c# - MDT EventRegister nuget 以代码 1 退出

c# - 如何使用 linq 从表中搜索任何属性?

c# - 如何使用 ASP.NET MVC 更改 ModelState 中的值,使其有效?

c# - 是否可以迭代两个容器?

python - 与 python3 asyncio 建立连接

C - 在服务器和客户端之间创建无限套接字连接 (Linux)