public SearchResultCollection FindAll(int count)
        {
            // TODO - Do something with count

            if (!PropertiesToLoad.Contains("ADsPath"))
            {
                PropertiesToLoad.Add("ADsPath");
            }

            SearchRequest req = new SearchRequest(SearchRoot.DistinguishedName, Filter, SearchScope, PropertiesToLoad.ToArray());

            // Use the SearchRoot's connection since it should already exist.
            // TODO - Learn more about AD to understand if it's better to create a new connection or to use an existing one.
            var res = SearchRoot.Connection.SendRequest(req) as SearchResponse;

            if (res.ResultCode != ResultCode.Success)
            {
                throw new InvalidOperationException($"Error connecting to Active Directory: {res.ResultCode.ToString()}: {res.ErrorMessage}");
            }

            return(new SearchResultCollection(SearchRoot, res.Entries, PropertiesToLoad));
        }
Esempio n. 2
0
        /// <summary>
        /// Queries for directory entries matching a given filter up to a specified count
        /// </summary>
        /// <param name="count">The number of entries to find (-1 for no limit)</param>
        /// <returns>A SearchResultCollection containing matching directory entries</returns>
        public SearchResultCollection FindAll(int count)
        {
            // Always load ADsPath so that DirectoryEntry entities can be created
            if (!PropertiesToLoad.Contains("ADsPath"))
            {
                PropertiesToLoad.Add("ADsPath");
            }

            // Create an LDAP SearchRequest using the root's name and the provided filter, scope, and properties
            var req = new SearchRequest(SearchRoot.DistinguishedName, Filter, SearchScope, PropertiesToLoad.ToArray());

            if (count > 0)
            {
                // Limit the result size, if necessary
                req.SizeLimit = count;
            }

            // Use the SearchRoot's connection since it should already exist.
            var res = SearchRoot.Connection.SendRequest(req) as SearchResponse;

            if (res.ResultCode != ResultCode.Success)
            {
                throw new InvalidOperationException($"Error connecting to Active Directory: {res.ResultCode.ToString()}: {res.ErrorMessage}");
            }

            // Create and return a SearchResultCollection with the returned entries
            return(new SearchResultCollection(SearchRoot, res.Entries, PropertiesToLoad));
        }