2009-10-01 107 views
2

比方说,我在寻找什么组/通讯组列表特定用户所属的Active Directory中

OU=Groups,DC=contaco,DC=com,ct 

我可以找到一个子OU中的所有群体,但只有这样,才能找到所有的团体用户'bobdole'所属的是让我看看每个组,看看他是否在'成员'字段。

不幸的是,当我查看用户'bobdole'时,我没有看到一个memberOf字段包含所有这些列表,因此我必须通过每个组\分发列表来查看他是哪个成员。

有没有更有效的方法来做到这一点?我在c#

回答

4

这将返回用户所属的所有角色(组)。

public string[] GetRolesForUser(DirectoryEntry user) 
{  
    user.RefreshCache(new string[] { "tokenGroups" }); 

    var irc = new IdentityReferenceCollection(user.Properties["tokenGroups"].Count); 
    foreach (byte[] sidBytes in user.Properties["tokenGroups"]) 
     irc.Add(new SecurityIdentifier(sidBytes, 0)); 

    var coll = new StringCollection(); 
    irc = irc.Translate(typeof(NTAccount)); 

    foreach (var ir in irc) 
    { 
     if (ir is NTAccount) 
     { 
      coll.Add(ir.ToString()); 
     } 
    } 
    var accounts = new string[coll.Count]; 

    coll.CopyTo(accounts, 0); 
    return accounts; 
} 
1

纠正我,如果我错了,但我敢肯定,“tokenGroups”不包含DistributionGroups,但只有SecurityGroups /角色。

+0

我现在面临同样的问题。任何更新? 谢谢 – 2010-09-02 14:24:05

相关问题