2010-04-08 134 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 
}