/// <summary> /// Connect to the given host on the port asynchronously /// </summary> /// <param name="host">The host to connect to</param> /// <param name="port">The port to use for communication</param> public async Task ConnectAsync(string host, int port) { // Don't reconnect if (_state == LDAPConnectionState.Connected) { return; } try { await _conn.ConnectAsync(host, port); if (_sslEnabled) { _raw = _conn.GetStream(); var options = new SslClientAuthenticationOptions { TargetHost = host, EnabledSslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls, ClientCertificates = null, LocalCertificateSelectionCallback = null, CertificateRevocationCheckMode = X509RevocationMode.NoCheck, RemoteCertificateValidationCallback = (sender, cert, chain, errors) => { // Accept all...bad idea return(true); }, ApplicationProtocols = new List <SslApplicationProtocol>() { SslApplicationProtocol.Http11 }, EncryptionPolicy = EncryptionPolicy.RequireEncryption, }; _transport = new SslStream(_raw); await(_transport as SslStream).AuthenticateAsClientAsync(options, CancellationToken.None); Reader = new LDAPReader(_transport); Writer = new LDAPWriter(_transport); } else { _raw = _conn.GetStream(); _transport = null; Reader = new LDAPReader(_raw); Writer = new LDAPWriter(_raw); } // Create the pump and start it _pump = new MessagePump(Reader, _raw, _log); _pump.Start(); _state = LDAPConnectionState.Connected; } catch (Exception e) { _state = LDAPConnectionState.Faulted; throw new LDAPException("Failed to connect", e); } }
/// <summary> /// public constructor used to establish streams /// </summary> /// <param name="sslEnabled"></param> /// <param name="log"></param> public LDAPConnection(bool sslEnabled, ILogger log) { _conn = new TcpClient(); _sslEnabled = sslEnabled; _transport = null; _state = LDAPConnectionState.NotInitialized; _log = log; }
public async Task CloseAsync() { // Should probably throw if this is not the case if (_state != LDAPConnectionState.Connected) { return; } await TryQueueOperation(new UnbindRequest(), CancellationToken.None); if (_pump != null) { await _pump.StopAsync(); _pump.Dispose(); } _state = LDAPConnectionState.Closed; }