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);
        }
Exemplo n.º 2
0
        private void DoSearch()
        {
            InitBlock();
            String[] attrs = new String[PropertiesToLoad.Count];
            PropertiesToLoad.CopyTo(attrs, 0);

            LdapSearchConstraints cons = _conn.SearchConstraints;

            if (SizeLimit > 0)
            {
                cons.MaxResults = SizeLimit;
            }
            if (ServerTimeLimit != DefaultTimeSpan)
            {
                cons.ServerTimeLimit = (int)ServerTimeLimit.TotalSeconds;
            }

            int connScope = LdapConnection.SCOPE_SUB;

            switch (_SearchScope)
            {
            case SearchScope.Base:
                connScope = LdapConnection.SCOPE_BASE;
                break;

            case SearchScope.OneLevel:
                connScope = LdapConnection.SCOPE_ONE;
                break;

            case SearchScope.Subtree:
                connScope = LdapConnection.SCOPE_SUB;
                break;

            default:
                connScope = LdapConnection.SCOPE_SUB;
                break;
            }
            LdapSearchResults lsc = _conn.Search(SearchRoot.Fdn,
                                                 connScope,
                                                 Filter,
                                                 attrs,
                                                 PropertyNamesOnly, cons);

            while (lsc.hasMore())
            {
                LdapEntry nextEntry = null;
                try
                {
                    nextEntry = lsc.next();
                }
                catch (LdapException e)
                {
                    switch (e.ResultCode)
                    {
                    // in case of this return codes exception should not be thrown
                    case LdapException.SIZE_LIMIT_EXCEEDED:
                    case LdapException.TIME_LIMIT_EXCEEDED:
                    case LdapException.REFERRAL:
                        continue;

                    default:
                        throw e;
                    }
                }
                DirectoryEntry     de    = new DirectoryEntry(_conn);
                PropertyCollection pcoll = new PropertyCollection();
//				de.SetProperties();
                de.Path = DirectoryEntry.GetLdapUrlString(_Host, _Port, nextEntry.DN);
                LdapAttributeSet attributeSet        = nextEntry.getAttributeSet();
                System.Collections.IEnumerator ienum = attributeSet.GetEnumerator();
                if (ienum != null)
                {
                    while (ienum.MoveNext())
                    {
                        LdapAttribute attribute     = (LdapAttribute)ienum.Current;
                        string        attributeName = attribute.Name;
                        pcoll[attributeName].AddRange(attribute.StringValueArray);
//						de.Properties[attributeName].AddRange(attribute.StringValueArray);
//						de.Properties[attributeName].Mbit=false;
                    }
                }
                if (!pcoll.Contains("ADsPath"))
                {
                    pcoll["ADsPath"].Add(de.Path);
                }
//				_SrchColl.Add(new SearchResult(de,PropertiesToLoad));
                _SrchColl.Add(new SearchResult(de, pcoll));
            }
            return;
        }