1

我一直在用这个敲打我的头很长一段时间,但无法让它工作。我有一个 LDAP 查询,我确实在 AD 用户和计算机中工作,但不知道如何在 C# 中以编程方式进行。

这是我在 AD 工具中正常工作的 LDAP 查询:(memberOf=CN=AccRght,OU=Groups,OU=P,OU=Server,DC=mydomain,DC=com)(objectCategory=user)(objectClass=user) (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;
}

谢谢你的帮助!

4

2 回答 2

1

如果您实际上正在寻找一种递归枚举组成员的方法,那么您可能需要使用 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

于 2010-01-03T14:04:23.883 回答
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 文章在 .NET Framework 3.5 中管理目录安全主体- S.DS.AM 的优秀介绍!:-)

于 2010-01-03T16:05:12.173 回答