foreach 循环在 31 个用户的列表中多次重复搜索结果 resultpropertycollection 的结果

foreach loop repeats result of searchresult resultpropertycollection more than once in a list of 31 users

static void Main(string[] args)
{
    try
    {
        DirectoryEntry entry = new DirectoryEntry(
            "LDAP://123.45.678.9:389/cn=TestGroup,ou=Groups,dc=test,dc=test2", 
            "uid=test_user, ou=test, dc=test,dc=test2", "test-abc", 
            AuthenticationTypes.None);

        // List<string> GroupMembers = new List<string>() { "first_last", "first_last" };
        StringCollection GroupMembers = new StringCollection();
        Object obj = entry.NativeObject;
        Console.WriteLine("login success");

        DirectorySearcher search = new DirectorySearcher(entry);
        search.PropertiesToLoad.Add("uniqueMember");
        search.PropertiesToLoad.Add("uid");
        search.PropertiesToLoad.Add("mail");
        search.SearchScope = SearchScope.Subtree;
        search.Filter = "(&(uniqueMember=*))";

        //SearchResultCollection resultCollection = search.FindAll();
        foreach (SearchResult result in search.FindAll())
        {
            ResultPropertyCollection resultProperty = result.Properties;

            foreach (string GroupMemberDN in resultProperty["uniqueMember"])
            {
                DirectoryEntry directoryMember = new DirectoryEntry(
                    "LDAP://123.45.678.9:389/" + GroupMemberDN, 
                    "uid=test_user, ou=test, dc=test2,dc=test3", "test-abc", 
                    AuthenticationTypes.None);

                PropertyCollection DirectoryMemberProperties = directoryMember.Properties;
                GroupMembers.Add(directoryMember.Properties["mail"][0].ToString());
                GroupMembers.GetEnumerator();

                foreach (string member in GroupMembers)
                {
                    Console.WriteLine(member);
                }
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message.ToString());
    }
}

foreach 循环在 31 个用户的列表中多次重复 searchresult / resultpropertycollection 的结果,因此控制台中打印的结果大约是 60-70 个用户,重复几次

例如:

test_user1@email.com
test_user1@email.com
test_user2@email.com
test_user3@email.com
test_user4@email.com
test_user5@email.com
test_user6@email.com
test_user7@email.com
test_user8@email.com
test_user9@email.com
test_user1@email.com
test_user1@email.com
test_user2@email.com
// and so on so forth in this pattern..

我只想检索在 cn=TestGroup 的 uniqueMembers 中分配的 31 个用户,像 Console.WriteLine(

每次我 运行 这个程序,它都能工作(是)但不正确。

这可能是因为您在其他两个 foreach 循环的每次迭代中都输出了整个 GroupMembers 集合。

相反,我们应该将输出 GroupMembers 信息的代码移动到控制台 外部 foreach 循环之后(在完成填充之后) ).

foreach (SearchResult result in search.FindAll())
{
    ResultPropertyCollection resultProperties = result.Properties;

    foreach (string groupMemberDN in resultProperties["uniqueMember"])
    {
        DirectoryEntry directoryMember = new DirectoryEntry(
            "LDAP://123.45.678.9:389/" + groupMemberDN, 
            "uid=test_user, ou=test, dc=test2,dc=test3", "test-abc", 
            AuthenticationTypes.None);

        GroupMembers.Add(directoryMember.Properties["mail"][0].ToString());
    }
}

// Now that our collection is fully populated, output it to the console
foreach (string member in GroupMembers)
{
    Console.WriteLine(member);
}