2011-12-12 24 views
17

Poniższy kod zawiera listę niektórych, ale nie wszystkich, grup Active Directory. Czemu?Wyświetlanie wszystkich grup Active Directory

Próbuję wyświetlić listę wszystkich grup zabezpieczeń, grup dystrybucyjnych, grup komputerów itp. Czy określono nieprawidłową wartość objectClass?

private static void ListGroups() 
{ 
    DirectoryEntry objADAM = default(DirectoryEntry); 
    DirectoryEntry objGroupEntry = default(DirectoryEntry); 
    DirectorySearcher objSearchADAM = default(DirectorySearcher); 
    SearchResultCollection objSearchResults = default(SearchResultCollection); 
    SearchResult myResult=null; 

    objADAM = new DirectoryEntry(LDAP); 
    objADAM.RefreshCache(); 
    objSearchADAM = new DirectorySearcher(objADAM); 
    objSearchADAM.Filter = "(&(objectClass=group))"; 
    objSearchADAM.SearchScope = SearchScope.Subtree; 
    objSearchResults = objSearchADAM.FindAll(); 

    // Enumerate groups 
    try 
    { 
     fileGroups.AutoFlush = true; 
     if (objSearchResults.Count != 0) 
     { 
      foreach (SearchResult objResult in objSearchResults) 
      { 
       myResult = objResult; 
       objGroupEntry = objResult.GetDirectoryEntry(); 
       Console.WriteLine(objGroupEntry.Name); 
       fileGroups.WriteLine(objGroupEntry.Name.Substring(3)); 
      } 
     } 
     else 
     { 
      throw new Exception("No groups found"); 
     } 
    } 
    catch (PrincipalException e) 
    { 
     fileErrorLog.AutoFlush = true; 
     fileErrorLog.WriteLine(e.Message + " " + myResult.Path); 
    } 
    catch (Exception e) 
    { 
     throw new Exception(e.Message); 
    } 
} 

Odpowiedz

41

Jeśli jesteś na .NET 3.5 lub nowszy, można użyć PrincipalSearcher i „Zapytanie przez przykład” główny aby wykonać wyszukiwanie:

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a GroupPrincipal 
GroupPrincipal qbeGroup = new GroupPrincipal(ctx); 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeGroup); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
} 

Jeśli nie już - absolutnie przeczytać artykuł MSDN Managing Directory Security Principals in the .NET Framework 3.5 który pokazuje ładnie jak najlepsze wykorzystanie nowych funkcji w System.DirectoryServices.AccountManagement

+0

Dzięki Marc - to działało. – cymorg

+1

Chciałbym encapsulate 'PrincipalContext',' GroupPrincipal' i 'PrincipalSearcher' w użyciu bloków, ponieważ są one jednorazowego użytku. –

2

filtr Try „(objectCategory = grupa)” Znaleziono rozwiązanie here

+0

Niestety Sergey, taki sam rezultat, nie wszystkie wymienione grupy. Odpowiedź od marc_s wydaje się działać (pod warunkiem, że jesteś na .NET 3.5 lub nowszym). – cymorg

+0

Link jest uszkodzony –

2
DirectoryEntry entry = new DirectoryEntry("ldap://ldap.gaurangjadia.com", "scott", "tiger"); 

DirectorySearcher dSearch = new DirectorySearcher(entry); 
dSearch.Filter = "(&(objectClass=group))"; 
dSearch.SearchScope = SearchScope.Subtree; 

SearchResultCollection results = dSearch.FindAll(); 

for (int i = 0; i < results.Count; i++) { 
    DirectoryEntry de = results[i].GetDirectoryEntry(); 

    //TODO with "de" 
} 
0

Próbowałem to i to działało

public ArrayList GetAllGroupNames(string ipAddress, string ouPath) 
    { 
     DirectorySearcher deSearch = new DirectorySearcher(); 
     deSearch.SearchRoot = GetRootDirectoryEntry(ipAddress, ouPath); 
     deSearch.Filter = "(&(objectClass=group))"; 
     SearchResultCollection results = deSearch.FindAll(); 
     if (results.Count > 0) 
     { 
      ArrayList groupNames = new ArrayList(); 

      foreach (SearchResult group in results) 
      { 
       var entry = new DirectoryEntry(group.Path, UserName, Password); 
       string shortName = entry.Name.Substring(3, entry.Name.Length - 3); 
       groupNames.Add(shortName); 
      } 

      return groupNames; 
     } 
     else 
     { 
      return new ArrayList(); 
     } 
    } 

    private DirectoryEntry GetRootDirectoryEntry(string ipAddress, string domainPath, string username, string password) 
    { 
     var ldapPath = "LDAP://" + ipAddress + "/" + domainPath; 
     return new DirectoryEntry(ldapPath, username, password, AuthenticationTypes.Secure); 
    } 
+0

Cześć Co to jest GetRootDirectoryEntry? – VAAA

+0

@VAAA, Edytowałem swoją odpowiedź –

Powiązane problemy