Esempio n. 1
0
        /// <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);
            }
        }
Esempio n. 2
0
        void Dispose(bool disposing)
        {
            if (disposing && !_isDisposed)
            {
                if (_pump != null)
                {
                    _pump.Dispose();
                }

                if (_transport != null)
                {
                    _transport.Flush();
                    _transport.Close();
                }
                else if (_raw != null)
                {
                    _raw.Flush();
                    _raw.Close();
                }

                if (_conn != null)
                {
                    _conn.Close();
                }

                _transport = null;
                _raw       = null;
                _conn      = null;
                _pump      = null;

                // Notify GC to ignore
                GC.SuppressFinalize(this);
            }

            _isDisposed = true;
        }