locked
User List From Active Directory RRS feed

  • Question

  • User-1991712009 posted

    I have the following code that returns a user's first and last name.

    How do I change the code to retrieve all users and then filter users instead of hard coding a user name?

                DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher("(samaccountname=johndoe)");
                 System.DirectoryServices.SearchResultCollection userlist=  searcher.FindAll(); 
                 for(int i=0;i<userlist.Count;i++) 
                 { 
                    userlist[i].Properties["displayName"].ToString(); 
                 }  
    

    Monday, February 4, 2013 6:56 PM

Answers

All replies

  • User281315223 posted

    This Microsoft Example using DirectorySearcher uses the following to get all of the users within a domain : 

    // Bind to the users container.
    DirectoryEntry entry = new DirectoryEntry("LDAP://CN=users,DC=fabrikam,DC=com");
    
    // Create a DirectorySearcher object.
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    
    // Create a SearchResultCollection object to hold a collection of SearchResults
    // returned by the FindAll method.
    SearchResultCollection result = mySearcher.FindAll();
    

    This StackOverflow discussion might be helpful as well, as it discusses the same question but provides a bit of explaination about using and querying the Active Directory.

    Monday, February 4, 2013 7:09 PM
  • User-1991712009 posted

    The string s won't return any values

    for (int i = 0; i < result.Count; i++)
                {
                    string s = result[i].Properties["displayname"].ToString();
                } 

    Monday, February 4, 2013 7:28 PM
  • User281315223 posted

    You can find a rather exhaustive list of all of the available properties / attributes for the Active Directory here.

    You may want to try using "displayName" (not sure about case-sensitivity in comparison to your current usage) or "lDAPDisplayName"

    string s = result[i].Properties["displayName"].ToString();     //Display-Name property
    string s = result[i].Properties["lDAPDisplayName"].ToString(); //LDAP-Display-Name property

    Monday, February 4, 2013 7:47 PM
  • User-1991712009 posted

    They both return:

    System.DirectoryServices.ResultPropertyValueCollection

    Monday, February 4, 2013 7:50 PM
  • User281315223 posted

    Have you tried removing the .ToString() after your properties or using the debugger and placing a breakpoint within the loop to see what kind of properties are available on  your result?

    (You can use the QuickWatch or Locals in Visual Studio to check the value of result[i].Properties["YourProperty"] etc.)

    Monday, February 4, 2013 8:24 PM
  • User-1991712009 posted

    Yes, I have and it did not work.

    Tuesday, February 5, 2013 8:30 AM
  • User281315223 posted

    Did you step through your code within your loop and check out your 'results' variable and see what kinds of properties it has? It can often be a great way to extract exactly what you need in situations like this.

    Tuesday, February 5, 2013 8:42 AM
  • User-1991712009 posted

    Yes, I have and it returns null.

    Tuesday, February 5, 2013 10:38 AM
  • User-1991712009 posted

     

    How do I change my original code to retrieve all users and then filter users instead of hard coding a user name?

    DirectorySearcher searcher = new System.DirectoryServices.DirectorySearcher("(samaccountname=johndoe)"); 
                 System.DirectoryServices.SearchResultCollection userlist=  searcher.FindAll(); 
                 for(int i=0;i<userlist.Count;i++) 
                 { 
                    userlist[i].Properties["displayName"].ToString(); 
                 }

    Tuesday, February 5, 2013 12:24 PM
  • User281315223 posted

    You may want to try using the PrincipleContext to accomplish this, as mentioned in this question on Stack Overflow. This link might be helpful as well.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 10, 2013 6:17 PM