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:
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..
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.