2010-04-08 132 views
0

我正在使用下面的代碼來獲取組中的成員。Active Directory組成員問題

private static List<string> GetGroupMembers(string groupName) 
{ 
    Tracer.LogEntrace(groupName); 

    List<string> retVal = new List<string>(); 

    GroupPrincipal groupPrincipal = 
    GroupPrincipal.FindByIdentity(
     new PrincipalContext(ContextType.Domain), 
     IdentityType.SamAccountName, 
     groupName); 

    PrincipalSearchResult<Principal> principleSearchResult = 
    groupPrincipal.GetMembers(true); 

    if (principleSearchResult != null) 
    { 
    try 
    { 
     foreach (Principal item in principleSearchResult) 
      retVal.Add(item.DistinguishedName); 
    } 
    catch (Exception ex) 
    { 
     Tracer.Log(ex.Message); 
    } 
    } 
    else 
    { 
    //Do Nothing 
    } 

    Tracer.LogExit(retVal.Count); 

    return retVal; 

}

它非常適用所有羣體,但同時 枚舉組發生時其來我得到以下

錯誤的用戶組「的錯誤(87) 。該組的 SID無法解決。「

+0

也許你的意思是 「域用戶」,而不是 「用戶」 – RobSiklos 2013-08-09 15:25:17

回答

1

Users在Active Directory中不是一組 - 這是一個容器。這就是爲什麼你不能像一個羣體那樣枚舉它 - 你必須像OU(組織單位)那樣列舉它。

喜歡的東西:

// bind to the "Users" container 
DirectoryEntry deUsers = new DirectoryEntry("LDAP://CN=Users,DC=yourcompany,DC=com"); 

// enumerate over its child entries 
foreach(DirectoryEntry deChild in deUsers.Children) 
{ 
    // do whatever you need to do to those entries 
} 
相關問題