在活动目录中查找特定用户所属的组/分发列表

Mat*_*att 2 ldap active-directory .net-3.5

假设我在

OU=Groups,DC=contaco,DC=com,ct
Run Code Online (Sandbox Code Playgroud)

我可以找到一个子 OU 中的所有组,但是找到用户“bobdole”所属的所有组的唯一方法是让我查看每个组,看看他是否在“成员”字段中。

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

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

mxm*_*ile 5

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

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;
}
Run Code Online (Sandbox Code Playgroud)