Search code examples
.netactive-directorydirectoryservicesuserprincipal

Getting members of an AD group where result type is a custom derived UserPrincipal


I've created my own derived UserPrincipal-type which is used to get some extended AD properties. This works fine.

Now I'm looking for a way to use the GetMembers() method of the groupprincipal object to return me a list of my custom UserPrincipal type.

A bit the same way FindByIdentityWithType works on the UserPrincipal where there's an overload on which you can specify your own PrincipalType.

Is there a way to do this on the GetMembers method?


Solution

  • I did not manage to find an easy way to return custom userprincipals from the GetMembers function. I wasn't even able to convert the returned UserPrincipal to my custom userprincipal.

    Finally I've solved this by getting all the users from my OU using the FindAll method on the PrincipalSearcher object and setting its queryfilter to a new type of my custom userprincipal.

    Then checking each user whether or not it is a member of a group by using the GetGroups method of the base userprincipal class.