【问题标题】:Find members and members of sub-group查找子组的成员和成员
【发布时间】:2014-03-01 09:35:58
【问题描述】:

如何使用 DirectorySearcher 和 Filter/PropertiesToLoad 获取特定组/部门内所有成员和(成员)子组的列表?

目前,我可以让该组返回其所有成员。但是,组内有子组,我也无法联系到这些子组中的成员。

这是我目前所拥有的:

        DirectoryEntry entry = null;
    entry = new DirectoryEntry("LDAP://DC=au,DC=company,DC=com", null, null, AuthenticationTypes.Secure);

    try
    {
        DirectorySearcher ouSearch = new DirectorySearcher(entry);
        ouSearch.Filter = "(&(objectClass=user)(objectCategory=person)(displayName=*" + username + "*)" +
        "(|" +
            "(memberOf=CN=my department,OU=ADC-Distribution Groups,DC=au,DC=company,DC=com)" +

        ")" +

        ")";

        ouSearch.PropertiesToLoad.Add("samAccountName");
        ouSearch.PropertiesToLoad.Add("displayName");
        ouSearch.PropertiesToLoad.Add("memberOf");
        ouSearch.SearchScope = SearchScope.Subtree;

        SearchResultCollection allOUS = ouSearch.FindAll();

感谢任何帮助!

【问题讨论】:

    标签: asp.net active-directory ldap directoryservices directorysearcher


    【解决方案1】:

    您必须递归地扩展属于另一个组的每个子组。

    但是,使用 System.DirectoryServices.AccountManagement 命名空间的 GroupPrincipal 类有更简单的方法。

    GroupPrincipal 有一个方法GetMembers,它允许您递归检索组的所有成员。您所要做的就是将true 指定为GetMembers 的唯一参数。

    以下示例复制自MSDN

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain,                                                                    
                                            "fabrikam.com", 
                                            "DC=fabrikam,DC=com", 
                                            "administrator", 
                                            "SecretPwd123");
    
    GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, 
                                                   IdentityType.Name, 
                                                   "Domain Admins");
    
    if (grp != null)
    {
        foreach (Principal p in grp.GetMembers(recursive: true))
        {
            Console.WriteLine(p.Name);
        }
        grp.Dispose();
    }
    
    ctx.Dispose(); 
    

    【讨论】:

    • 非常感谢 Jakob....我几天来一直在寻找解决方案,而这正是我所追求的。非常感谢您的回复!
    • 你知道是否有一些名称过滤可以用于 GroupPrincipal 类吗?我的 getMembers() 返回超过 1k 个成员,这需要很长时间 - 我需要遍历每个成员并检查他们的名字是否与特定模式匹配。 Atm,每个循环都需要 10 秒!
    • @viv_acious:我不认为你可以这样过滤。但是为什么不使用 PrincipalSearcher 而不是 GetMembers 来搜索用户呢?
    猜你喜欢
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多