Exemple #1
0
        /// <summary>
        /// Executes the search and returns only the first entry that is found.
        /// </summary>
        /// <param name="directoryEntry">The node in the Active Directory Domain Services hierarchy where the search starts.</param>
        /// <param name="filter">The search filter string in Lightweight Directory Access Protocol (LDAP) format.</param>
        /// <param name="propertiesToLoad">The set of properties to retrieve during the search.</param>
        /// <returns>A SearchResult object that contains the first entry that is found during the search.</returns>
        public static SearchResult FindOne(DirectoryEntry directoryEntry, string filter, string[] propertiesToLoad = null)
        {
            SearchResult response;

            var settings    = LdapConfigurationsHelper.GetSettings();
            var searchScope = (SearchScope)Enum.Parse(typeof(SearchScope), settings.Scope);

            using (var searcher = new DirectorySearcher(directoryEntry, filter)
            {
                SearchScope = searchScope, PageSize = 1
            })
            {
                if (propertiesToLoad != null)
                {
                    var padlock = new object();

                    Parallel.ForEach(propertiesToLoad, item =>
                    {
                        lock (padlock)
                            searcher.PropertiesToLoad.Add(item);
                    });
                }

                response = searcher.FindOne();
            }

            return(response);
        }
Exemple #2
0
        /// <summary>
        /// Executes the search and returns a collection of the entries that are found.
        /// </summary>
        /// <param name="directoryEntry">The node in the Active Directory Domain Services hierarchy where the search starts.</param>
        /// <param name="filter">The search filter string in Lightweight Directory Access Protocol (LDAP) format.</param>
        /// <param name="propertiesToLoad">The set of properties to retrieve during the search.</param>
        /// <param name="sortOption">Gets or sets a value indicating the property on which the results are sorted.</param>
        /// <param name="pageSize">Gets or sets a value indicating the page size in a paged search.</param>
        /// <param name="sizeLimit">Gets or sets a value indicating the maximum number of the objects that the server returns in a search.</param>
        /// <returns>A SearchResultCollection object that contains the results of the search.</returns>
        public static SearchResultCollection FindAll(DirectoryEntry directoryEntry, string filter, string[] propertiesToLoad = null, SortOption sortOption = null, int pageSize = 0, int sizeLimit = 0)
        {
            if (pageSize < 0)
            {
                throw new ArgumentException("Page size cannot be less than 0!", nameof(pageSize));
            }
            if (sizeLimit < 0)
            {
                throw new ArgumentException("Size limit cannot be less than 0!", nameof(sizeLimit));
            }

            SearchResultCollection response;

            if (sortOption == null)
            {
                sortOption = new SortOption();
            }

            var settings    = LdapConfigurationsHelper.GetSettings();
            var searchScope = (SearchScope)Enum.Parse(typeof(SearchScope), settings.Scope, true);

            using (var searcher = new DirectorySearcher(directoryEntry, filter)
            {
                SearchScope = searchScope, PageSize = pageSize, SizeLimit = sizeLimit, Sort = sortOption
            })
            {
                if (propertiesToLoad != null)
                {
                    var padlock = new object();

                    Parallel.ForEach(propertiesToLoad, item =>
                    {
                        lock (padlock)
                            searcher.PropertiesToLoad.Add(item);
                    });
                }

                response = searcher.FindAll();
            }

            return(response);
        }