0

如何使用 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();

任何帮助表示赞赏!

4

1 回答 1

1

您将不得不递归地扩展作为另一个组成员的每个子组的搜索。

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

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(); 
于 2014-02-14T10:46:24.483 回答