I need to find all users that are members of two groups (GroupA and GroupB). I also need to take into account nested groups. What is the best way to do this?
I kno
Is there a requirement that you use ldap search to do this? The WindowsPrincipal.IsInRole() method will test for membership both directly and via a nested group - at least it did for the test I ran.
This code tests the current thread's identity against GroupA and GroupB but you could use a similar approach to enumerate the members of GroupA and then test each of those against GroupB by calling IsInRole...
AppDomain myDomain = Thread.GetDomain();
myDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
NTAccount groupA = new NTAccount("Domain\\GroupA");
SecurityIdentifier sidGroupA = (SecurityIdentifier)groupA.Translate(typeof(SecurityIdentifier));
bool inGroupA = myPrincipal.IsInRole(sidGroupA);
NTAccount groupB = new NTAccount("Domain\\GroupB");
SecurityIdentifier sidGroupB = (SecurityIdentifier)groupB.Translate(typeof(SecurityIdentifier));
bool inGroupB = myPrincipal.IsInRole(sidGroupB);
Console.WriteLine("{0}, {1}", groupA, inGroupA);
Console.WriteLine("{0}, {1}", groupB, inGroupB);
Console.ReadLine();