locked
Issues in Getting Users from Active Directory RRS feed

  • Question

  • User-372693909 posted

    Hi,

    I am trying to retrieve the users from Active Directory using below code. It returns all the users under "Users" directory but i have few users under "Domain Controllers" directory how to retrieve those users. Please help.

                    DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);   
                    var directorySearcher = new DirectorySearcher(entry);
                    directorySearcher.Filter = "(&(objectClass=user))";
                    var propertiesToLoad = new[] 
                                                   { 
                                                       "SAMAccountName", 
                                                       "displayName", 
                                                       "givenName", 
                                                       "mail"
                                                    };
                    directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad);
    
                    foreach (SearchResult searchEntry in directorySearcher.FindAll())
                    {
                        var userEntry = searchEntry.GetDirectoryEntry();
                       
                        var displayName = userEntry.Properties["displayName"].Value;
    
                        if (displayName == null)
                            continue;
                        var samAccName = userEntry.Properties["SAMAccountName"].Value;
                        var email = userEntry.Properties["mail"].Value;
                        var givenName = userEntry.Properties["givenName"].Value;
                     }

     Thanks in advance.

    Sivananda Vanukuri

    Wednesday, March 27, 2013 2:59 AM

Answers

  • User197322208 posted
    Talk with system admin
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 28, 2013 7:47 AM

All replies

  • User197322208 posted

    It returns all the users under "Users" directory but i have few users under "Domain Controllers" directory how to retrieve those users

    modify the _path

    entry = new DirectoryEntry(_path, domainAndUsername, pwd); 
    

    Wednesday, March 27, 2013 12:08 PM
  • User-372693909 posted

    Hi,

    Thanks for the reply. When i tried like that the error wass "there is no such object on the server". Below are the _path values for users and domain controllers.

    For Users
    
    "LDAP://xyz.com/CN=Users,DC=xyz,DC=com"
    
    
    For Domain Controllers
    
    "LDAP://xyz.com/CN=DomainControllers,DC=xyz,DC=com"   (OR)
    
    ""LDAP://xyz.com/CN=Domain Controllers,DC=xyz,DC=com"

    Please guide me on what mistake am i doing?

    Thanks in advance

    Thursday, March 28, 2013 12:56 AM
  • User197322208 posted
    Talk with system admin
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 28, 2013 7:47 AM
  • User-372693909 posted

    Instead of CN (or) used OU to connect to Domain Controller. Thanks for the help.

    Monday, April 1, 2013 5:18 AM