Search code examples
c#active-directoryldapdirectorysearcher

C# Active Directory DirectorySearcher is only finding my own user


I have a function that's meant to look up the displayName and mail fields of any users that match the given name. So far it works but it only ever returns the user that I log in as.

I've already tried different versions of DirectorySearcher, like this:

new DirectorySearcher("LDAP://my.domain");
new DirectorySearcher();
new DirectorySearcher("LDAP://my.domain/(&(objectCategory=person)(objectClass=user)(anr={0}))");

I've also tried different path formats and AuthenticationTypes.

My Function:

/// <summary>
/// Returns a Dictionary of Names and Emails that match the given name
/// </summary>
/// <param name="name">Name to search for</param>
/// <param name="domain">Domain to log in to</param>
/// <param name="username">Username for login into AD</param>
/// <param name="pwd">Password for login into AD</param>
/// <param name="count">returns the number of results found</param>
/// <returns>Dictionary containing the Names and Emails of the users matched</returns>
public Dictionary<string, string> GetPersonsEmailsByName(string name, string domain, string username, string pwd, out int count)
{
    count = 0;
    if (String.IsNullOrEmpty(name)) return new Dictionary<string, string>();

    try
    {
        string domainAndUsername = String.Format(@"{0}\{1}", domain, username);
        using (DirectoryEntry root = new DirectoryEntry("LDAP://my.domain", domainAndUsername, pwd, AuthenticationTypes.Delegation))
        {
            DirectorySearcher search = new DirectorySearcher(root);
            search.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(anr={0}))", name);
            search.SearchScope = SearchScope.Subtree;
            search.PropertiesToLoad.Add("displayName");
            search.PropertiesToLoad.Add("mail");
            var dict = new Dictionary<string, string>();

            SearchResultCollection result = search.FindAll();
            count = result.Count;
            foreach (SearchResult sr in result)
            {
                var de = sr.GetDirectoryEntry();
                dict.Add((string)de.Properties["displayName"].Value, (string)de.Properties["mail"].Value);
            }
            return dict;
        } 

    }
    catch (Exception ex)
    {
        throw new Exception("Error obtaining persons: " + ex.Message);
    }

}

And here is a sample output using this function:

User: john_doe
Pass:
Auth OK
Name: Mike
Results: 0
Name: carl
Results: 0
Name: jo
Results: 1
John Doe: [email protected]
Name: john
Results: 1
John Doe: [email protected]
Name: j
Results: 1
John Doe: [email protected]
Name:

All I can ever get is my own name.

Thanks in advance for your help.


Solution

  • Turns out it was all my fault. I did a login with another function earlier in the code which unknowingly to me changed the path variable I was using to the cn of the logged user, that's why this was not giving me any other results other than the logged in user.

    So this method works exactly as it should!

    My apologies.