0
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());
    }
}

the foreach loop repeats result of searchresult / resultpropertycollection more than once in a list of 31 users, such that the printed result in console is about 60-70 users, repeated a few times

for example:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
// and so on so forth in this pattern..

I'd just to retrieve the 31 users assigned in uniqueMembers of the cn=TestGroup to be printed once like with Console.WriteLine(

Everytime I run the Program, it works (yes) but not correctly.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Subah
  • 45
  • 9
  • FYI, `List` is favored now over `StringCollection`. You can read the answer to [this question](https://stackoverflow.com/questions/669106/best-string-container-stringcollection-collectionstring-liststring-array) for more info – Rufus L Aug 23 '18 at 21:25

1 Answers1

1

This is likely because you're outputting the entire GroupMembers collection on each iteration of both the other two foreach loops.

Instead we should move the code that outputs the GroupMembers information to the console after the outer foreach loop (after you've finished populating it).

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);
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43