2011-12-12 124 views
17

以下代碼列出了一些(但不是全部)Active Directory組。爲什麼?列出所有Active Directory組

我想列出所有安全組,通訊組,計算機組等。我指定了錯誤的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); 
    } 
} 

回答

41

如果你在.NET 3.5或更新的,則可以使用一個PrincipalSearcher和一個「查詢通過例如」主做你的搜索:

// 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.....   
} 

如果您尚未 - 絕對看MSDN文章Managing Directory Security Principals in the .NET Framework 3.5這很好地說明如何使新功能的最佳使用System.DirectoryServices.AccountManagement

+0

謝謝馬克 - 它工作的一種享受。 – cymorg

+1

我會封裝'PrincipalContext','GroupPrincipal'和'PrincipalSearcher'使用塊,因爲它們是一次性的。 –

2

嘗試過濾器「(objectcategory =基團)」 實測溶液here

+0

對不起謝爾蓋,同樣的結果,沒有列出所有組。從marc_s回答似乎工作(只要你在.NET 3.5或更高版本)。 – cymorg

+0

鏈接被破壞 –

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

我想這和它的工作

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

嗨什麼是GetRootDirectoryEntry? – VAAA

+0

@VAAA,我編輯了我的答案 –

相關問題