• How to retrieve Active Directory User properties using LDAP

    Posted on June 29, 2012 by in Dotnet Tips & Tricks, Tips & Tricks

    public static void RetrieveUser(string username)
    {
        //active directory properties attribute names
        string propUsername = "samaccountname";
        string propFirstName = "givenName";
        string propLastName = "sn";
        string propDisplayName = "cn";
        string propMail = "mail";
        string propGuid = "objectguid";
    
        //set active directory search base
        DirectoryEntry directoryEntry = new DirectoryEntry(CONST_DOMAIN_PATH, CONST_AD_USERNAME, CONST_AD_PASSWORD);
    
        using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry))
        {
            //set the list of properties you are interested in
            //DOMAIN PATH PATTERN: CN=Name,CN=Directory,DC=Domain,DC=com or simply server name
            directorySearcher.PropertiesToLoad.Add(propUsername);
            directorySearcher.PropertiesToLoad.Add(propDisplayName);
            directorySearcher.PropertiesToLoad.Add(propFirstName);
            directorySearcher.PropertiesToLoad.Add(propLastName);
            directorySearcher.PropertiesToLoad.Add(propMail);
            directorySearcher.PropertiesToLoad.Add(propGuid);
    
            //Set a filters to get only the active users from AD
            directorySearcher.Filter = string.Format("({0})", "&(objectClass=user)(cn=" + username + ")");
    
            //Set Search Options
            directorySearcher.SearchScope = SearchScope.Subtree;
            directorySearcher.SearchRoot.AuthenticationType = AuthenticationTypes.Secure;
            directorySearcher.PageSize = 100;
    
            //run the search and and it will a collection of the entries that are found.
            using (SearchResultCollection results = directorySearcher.FindAll())
            {
                foreach (SearchResult result in results)
                {
                    //get poperties and write them to the console
                    if (result.Properties.Contains(propUsername))
                    {
                        Console.WriteLine("User Name: " + result.Properties[propUsername][0]);
                    }
                    if (result.Properties.Contains(propGuid))
                    {
                        Console.WriteLine("User GUID: " + BitConverter.ToString((byte[])result.Properties[propGuid][0]).Replace("-", string.Empty));
                    }
                    if (result.Properties.Contains(propMail))
                    {
                        Console.WriteLine("Mail ID: " + result.Properties[propMail][0]);
                    }
                    if (result.Properties.Contains(propDisplayName))
                    {
                        Console.WriteLine("DisplayName: " + result.Properties[propDisplayName][0]);
                    }                        
                }
            }
            //release resources
            directorySearcher.Dispose();
            directoryEntry.Dispose();
        }
    }
    
    Be Sociable, Share!
      Post Tagged with

    Written by

    View all articles by

    Email : [email protected]

    Leave a Reply