I want to create a simple app that is showing recursive group membership, however not users but groups. So if groupA have groupB as a member and groupB have groupC as a member, I want the code to show me all the groups nested till the last one. Here is the sample with what I have:
if (member.Any())
{
List<string> tempGroup = new List<string>();
do
{
tempGroup.Clear();
foreach (object nestedGroup in member)
{
SearchResult NestedGroupMemberResult = null;
try
{
string NestedgroupMemberSearchFilter = "(&(objectClass=group)(distinguishedName=" + nestedGroup.ToString() + "))";
string NestedgroupMemberPropertise = "member,distinguishedName";
NestedGroupMemberResult = dirSearcher(NestedgroupMemberSearchFilter, NestedgroupMemberPropertise);
}
catch { }
if(NestedGroupMemberResult.Properties["member"].Count > 0)
{
foreach (object NestedGroupMember in NestedGroupMemberResult.Properties["member"])
{
member.Add(NestedGroupMember.ToString());
tempGroup.Add(NestedGroupMember.ToString());
}
}
}
} while (!tempGroup.Any());
}
listBox1.DataSource = member;
Basically member is a list which contains group members from an input group. Lets say that I initial put GroupA and it have members as GroupB and GroupC and both will be in member list. Now I would like that loop do..while searches for each member of input group using LDAP filter applied, and if it finds any group add it to both member list and tempGroup list, so each time there is a nested group, member list will be updated and at the same time it will be included in foreach loop. If app finds out that there are no more nested groups, the tempGroup list will be empty and loop will stop.
However I'm constantly receiving error "System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'" for
foreach (object nestedGroup in member)
And I guess I cannot modify member list while for loop is iterating through it. I had very similar loop in powershell that works great, but I wanted to have an c# app as I know that it can be much faster in such searches.