Search in root/subtree active directory users

jwradhe

Member
Joined
Sep 23, 2021
Messages
7
Programming Experience
Beginner
Hi!
Im having some problem in my function to search on all users in my active directory, root and subtree.

my ad is like this:

ad.company
-> se.ad.company
-> fin.ad.company
-> no.ad.company

I can search , but only get some results from ad.company, not the other subtree directorys.
What have i done wrong?

And this is my code:

C#:
private void AutoCompleteTextBox_ReferenceUser()
        {

            try
            {
                var attributeName = "sn";
                string OU = "DC=ad,DC=company";
                var searchString = textBox_manager.Text;
                var ent = new DirectoryEntry("GC://" + OU);
                var mySearcher = new DirectorySearcher(ent);
                mySearcher.Filter = string.Format("(&(anr={0})(objectCategory=user)(objectClass=user))", attributeName, searchString);

                SearchResultCollection result = mySearcher.FindAll();

                List<string> names = new List<string>();

                foreach(SearchResult sr in result)
                    {
                        var n = sr.Properties["cn"][0].ToString();
                        if (!names.Contains(n))
                        {
                            stringCollection.Add(n);
                        }
                    }

                textBox_referenceuser.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBox_referenceuser.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox_referenceuser.AutoCompleteCustomSource = stringCollection;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
As I recall, Active Directory domains don't always act like Internet subdomains. Although from the end user, it looks like everything is unified under "ad.company", doing a search requires more work on the programmers.

Before digging too far, I suggest running "AD Explorer" (I think it's still available for download.), and see what query it builds when you do the same search using their search dialog.

(I'll try to move this thread someplace more appropriate, since this is really an AD issue, not a WinForms issue.)
 

Latest posts

Back
Top Bottom