In my Active Directory (my.domain) I have many groups (UserGrp1, UserGrp2, etc.) in which there are many users. A user can exist in several groups. I currently have code that allows me to use the GroupPrincipal class to search for a group, and then from there to get all the members of this group (see code below). However, I really need to find all the groups the user belongs to. For example, I have a domain user named Joe Test (sAMAccountName = JOETEST), and I need to find all the groups to which it belongs. What is the best way to do this?
I can determine if the user belongs to a group (as shown below) if I go through all the members returned by the GetMembers () method, but this seems ineffective to me and I would be surprised if there wasnβt a more efficient way.
using (PrincipalContext ctx = new PrincipalContext( ContextType.Domain, "my.domain", "DC=my,DC=domain")) { if (ctx != null) { using (GroupPrincipal gp = GroupPrincipal.FindByIdentity(ctx, "UserGrp1")) {
Thanks in advance for any help I get from this.
source share