在LDAP中搜索给定目录中的子目录

时间:2013-06-04 02:02:28

标签: c# ldap

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

它确实连接了,但现在我不确定如何在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())
{
}

谢谢你的时间!

1 个答案:

答案 0 :(得分: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中的用户和群组一起玩!