private static LdapConnection Connect([NotNull] IntegrationOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var ldapConnection = new LdapConnection {
                SecureSocketLayer = false
            };

            ldapConnection.Connect(options.Host, options.Port);
            ldapConnection.Bind(options.ProtocolVersion, options.Login, options.Password);
            Debug.WriteLine($@"Connected to <{options.Host}:{options.Port}> as <{options.Login}>");
            return(ldapConnection);
        }
        private List <T> RetrievePage([NotNull] LdapConnection ldapConnection, [NotNull] IntegrationOptions options, out LdapControl[] responseControls)
        {
            if (ldapConnection == null)
            {
                throw new ArgumentNullException(nameof(ldapConnection));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var mappedPageResults = new List <T>();
            var searchResults     = ldapConnection.Search
                                    (
                options.SearchBase,
                LdapConnection.ScopeSub,
                options.Filter,
                options.TargetAttributes,
                false,
                (LdapSearchConstraints)null
                                    );

            while (searchResults.HasMore())
            {
                try
                {
                    var nextEntry   = searchResults.Next();
                    var mappedEntry = _converter.Invoke(nextEntry);
                    mappedPageResults.Add(mappedEntry);
                }
                catch (LdapException ex)
                {
                    // you may want to turn referral chasing on
                    if (ex is LdapReferralException)
                    {
                        continue;
                    }
                    throw new InvalidOperationException("Failed to proceed to the next search result", ex);
                }
            }

            responseControls = ((LdapSearchResults)searchResults).ResponseControls;
            return(mappedPageResults);
        }
        public List <T> LoadAllPagedResults([NotNull] IntegrationOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            using var ldapConnection = Connect(options);

            var searchResult        = new List <T>();
            var isNextPageAvailable = PrepareForNextPage(ldapConnection, null, options.ResultPageSize, true);

            while (isNextPageAvailable)
            {
                var employeesPage = RetrievePage(ldapConnection, options, out var pageResponseControls);
                searchResult.AddRange(employeesPage);
                isNextPageAvailable = PrepareForNextPage(ldapConnection, pageResponseControls, options.ResultPageSize, false);
            }

            return(searchResult);
        }