1

我想找到给定目录的子目录。到目前为止我的代码看起来像这样..

它确实连接,但现在我不知道如何让组下MainGroup

DirectoryEntry _de = new DirectoryEntry("LDAP://xxx.com/DC=xxx,DC=org");

DirectorySearcher ds = new DirectorySearcher(_de);

ds.Filter = "(&(objectClass=group)(CN=MainGroup)";

ds.SearchScope = SearchScope.Subtree;           
ds.PageSize = 1000;
ds.SizeLimit = 0;

foreach (SearchResult result in ds.FindAll())
{
}

谢谢你的时间!

4

1 回答 1

1

如果您使用的是 .NET 3.5 及更高版本,则应查看System.DirectoryServices.AccountManagement(S.DS.AM) 命名空间。在这里阅读所有相关信息:

基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find the group in question
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "MainGroup");

    // if found....
    if (group != null)
    {
        // iterate over members
        foreach (Principal p in group.GetMembers())
        {
             Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
             // do whatever you need to do to those members

             // if you need to find the groups that are members of 'MainGroup'  
             GroupPrincipal group = p as GroupPrincipal;
             if(group != null)
             {
                 // now you have a group that is member of 'MainGroup' - do what you need here
             }

        }
    }
}

新的 S.DS.AM 使得在 AD 中与用户和组一起玩变得非常容易!

于 2013-06-04T04:13:23.960 回答