public void TestGetUsersFromLdap()
 {
     IUserDirectoryReader ldapReader = new LDAPReader();
     ldapReader.Initialize(ConfigurationManager.AppSettings);
     IList<DirectoryUser> ldapUsers = ldapReader.GetUsers();
     Assert.AreEqual(4,ldapUsers.Count);
 }
示例#2
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);
            }
        }
        public void TestGetUsersFromLdap()
        {
            IUserDirectoryReader ldapReader = new LDAPReader();

            ldapReader.Initialize(ConfigurationManager.AppSettings);
            IList <DirectoryUser> ldapUsers = ldapReader.GetUsers();

            Assert.AreEqual(4, ldapUsers.Count);
        }
示例#4
0
        private static Manager CreateManager()
        {
            V1Instance           v1          = Factory.GetV1Instance();
            SmtpAdapter          smtpAdapter = Factory.GetSmtpAdaptor();
            IUserDirectoryReader ldapReader  = new LDAPReader();

            ldapReader.Initialize(ConfigurationManager.AppSettings);
            return(new Manager(v1, smtpAdapter, ldapReader));
        }
示例#5
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="reader">The reader to pump messages from</param>
 /// <param name="raw">The raw stream backing the reader</param>
 /// <param name="log"></param>
 public MessagePump(LDAPReader reader, NetworkStream raw, ILogger log)
 {
     _reader = reader;
     _raw    = raw;
     _head   = new MessagePumpNode();
     _tail   = null;
     _event  = new AutoResetEvent(false);
     _log    = log;
 }
示例#6
0
        /// <summary>
        /// Ensure the reader has additional contents
        /// </summary>
        /// <param name="reader">The reader to test</param>
        /// <param name="tag">The tag to check</param>
        internal static async Task GuardAsync(this LDAPReader reader, int tag)
        {
            if (!await reader.ReadAsync())
            {
                throw new LDAPProtocolException("Invalid response object");
            }

            if (reader.Tag != tag)
            {
                throw new LDAPProtocolException(string.Format("Invalid tag : {0} (expected {1})", reader.Tag, tag));
            }
        }
示例#7
0
        public override async Task ReadContentsAsync(LDAPReader reader)
        {
            var msgReader = reader.CreateReader();

            await msgReader.ReadAsync();

            DistinguishedName = await msgReader.ReadAsStringAsync();

            if (await msgReader.ReadAsync())
            {
                Attributes = (await msgReader.ReadPartialListAsync()).ToArray();
            }
        }
示例#8
0
        public virtual async Task ReadContentsAsync(LDAPReader reader)
        {
            // Validate the state of the reader
            if (reader.Tag != (int)Operation || reader.Scope != EncodingScope.APPLICATION)
            {
                throw new LDAPProtocolException("Invalid cast to response");
            }
            var contentReader = reader.CreateReader();
            await contentReader.GuardAsync((int)EncodingType.ENUMERATED);

            ResultCode = await contentReader.ReadAsIntAsync();

            await contentReader.GuardAsync((int)EncodingType.OCTET_STRING);

            MatchedDN = await contentReader.ReadAsStringAsync();

            await contentReader.GuardAsync((int)EncodingType.OCTET_STRING);

            DiagnosticMessage = await contentReader.ReadAsStringAsync();

            // check for more data
            if (await contentReader.ReadAsync())
            {
                // May be optional referral or additional data from request
                if (contentReader.Tag == 3 && contentReader.Scope == EncodingScope.CONTEXT_SPECIFIC)
                {
                    // Read the referral
                    await contentReader.SkipAsync();

                    if (await contentReader.ReadAsync())
                    {
                        await ReadResponseAsync(contentReader);
                    }
                }
                else
                {
                    await ReadResponseAsync(contentReader);
                }
            }
        }
示例#9
0
 protected virtual Task ReadResponseAsync(LDAPReader reader) => Task.CompletedTask;