Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        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));
        }
        private SearchResultCollection FindAll(bool findMoreThanOne)
        {
            //if it is not of valid searching type, then throw an exception
            if (SearchRoot == null)
            {
                Version adsVersion = GetAdsVersion();
                throw new InvalidOperationException(Res.GetString(Res.DSVersion, adsVersion.ToString()));
            }

            DirectoryEntry clonedRoot = null;

            if (assertDefaultNamingContext == null)
            {
                clonedRoot = SearchRoot.CloneBrowsable();
            }
            else
            {
                //SECREVIEW: If the SearchRoot was created by this object
                //                        it is safe to assert its browse permission to get
                //                        the inner IAds.
                DirectoryServicesPermission dsPermission = new DirectoryServicesPermission(
                    DirectoryServicesPermissionAccess.Browse, assertDefaultNamingContext);
                dsPermission.Assert();
                try {
                    clonedRoot = SearchRoot.CloneBrowsable();
                }
                finally {
                    DirectoryServicesPermission.RevertAssert();
                }
            }

            UnsafeNativeMethods.IAds adsObject = clonedRoot.AdsObject;
            if (!(adsObject is UnsafeNativeMethods.IDirectorySearch))
            {
                throw new NotSupportedException(Res.GetString(Res.DSSearchUnsupported, SearchRoot.Path));
            }

            UnsafeNativeMethods.IDirectorySearch adsSearch = (UnsafeNativeMethods.IDirectorySearch)adsObject;
            SetSearchPreferences(adsSearch, findMoreThanOne);

            string[] properties = null;
            if (PropertiesToLoad.Count > 0)
            {
                if (!PropertiesToLoad.Contains("ADsPath"))
                {
                    // if we don't get this property, we won't be able to return a list of DirectoryEntry objects!
                    PropertiesToLoad.Add("ADsPath");
                }
                properties = new string[PropertiesToLoad.Count];
                PropertiesToLoad.CopyTo(properties, 0);
            }

            IntPtr resultsHandle;

            if (properties != null)
            {
                adsSearch.ExecuteSearch(Filter, properties, properties.Length, out resultsHandle);
            }
            else
            {
                adsSearch.ExecuteSearch(Filter, null, -1, out resultsHandle);
                properties = new string[0];
            }

            SearchResultCollection result = new SearchResultCollection(clonedRoot, resultsHandle, properties, Filter);

            return(result);
        }