【问题标题】:LDAP Query with sub result带有子结果的 LDAP 查询
【发布时间】:2010-01-03 13:08:39
【问题描述】:

我已经为此苦苦挣扎了很长一段时间,但无法让它发挥作用。我有一个 LDAP 查询,我确实在 AD 用户和计算机中工作,但不知道如何在 C# 中以编程方式进行。

这是我在 AD 工具中正常工作的 LDAP 查询:(memberOf=CN=AccRght,OU=Groups,OU=P,OU=Server,DC=mydomain,DC=com)(objectCategory=user)(objectClass =用户)(l=城市)

我已使用此代码获取用户帐户以获取 CN=AccRght 的成员,但我没有成功限制属于特定城市的用户。

public StringCollection GetGroupMembers(string strDomain, string strGroup)
{
    StringCollection groupMemebers = new StringCollection();
    try
    {
        DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
        DirectorySearcher srch = new DirectorySearcher("(CN=" + strGroup + ")");
        SearchResultCollection coll = srch.FindAll();
        foreach (SearchResult rs in coll)
        {
            ResultPropertyCollection resultPropColl = rs.Properties;
            foreach( Object memberColl in resultPropColl["member"])
            {
                DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                object obVal = userProps["sAMAccountName"].Value;
                if (null != obVal)
                {
                    groupMemebers.Add(obVal.ToString());
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.Write(ex.Message);
    }
    return groupMemebers;
}

感谢您的帮助!

【问题讨论】:

  • 您使用的是什么版本的 .NET? LDAP 可能不是必需的,有利于使用 System.DirectoryServices.AccountManagement 命名空间。
  • 我使用的是 .NET 3.5。我去看看 System.DirectoryServices.AccountManagement,看起来很有趣!
  • DirectoryServices.AccountManagement 方式更易于使用,对我来说更合乎逻辑。感谢您的提示!

标签: c# active-directory ldap directoryservices


【解决方案1】:

好吧,基本上您只需将您在工具中使用的 LDAP 过滤器转移到您的 DirectorySearcher 中 - 如下所示:

public StringCollection GetGroupMembers(string strDomain, string strGroup)
{
    StringCollection groupMemebers = new StringCollection();

    try
    {
        DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");

        DirectorySearcher srch = new DirectorySearcher();

        // build the LDAP filter from your (CN=strGroup) part that you had
        // in the constructor, plus that filter you used in the AD tool
        // to "AND" those together, use the LDAP filter syntax:
        //  (&(condition1)(condition2))  
        srch.Filter = string.Format("(&(CN={0})(memberOf=CN=AccRght,OU=Groups,OU=P,OU=Server,DC=mydomain,DC=com)(objectCategory=user)(objectClass=user)(l=City))", strGroup);

        SearchResultCollection coll = srch.FindAll();

        foreach (SearchResult rs in coll)
        {
            ResultPropertyCollection resultPropColl = rs.Properties;

            foreach( Object memberColl in resultPropColl["member"])
            {
                DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                object obVal = userProps["sAMAccountName"].Value;
                if (null != obVal)
                {
                    groupMemebers.Add(obVal.ToString());
                }
            }
        }
    }
    catch (Exception ex)
    {
        Console.Write(ex.Message);
    }
    return groupMemebers;
}

这应该将该过滤器应用于您的搜索,例如您现在应该只取回该特定城市的用户。

一定要查看这篇 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5 - S.DS.AM 的精彩介绍! :-)

【讨论】:

    【解决方案2】:

    如果您实际上正在寻找一种递归枚举组成员的方法,也许您需要使用 memberof 的递归版本(您可以使用 (memberof:1.2.840.113556.1.4.1941:=(cn=Group1,OU=groupsOU,DC=x))) 语法来实现)。

    更多信息在这里:http://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多