Instead of GroupPrincipal.GetMembers, I ended up using AdvancedSearchFilter to construct the LDAP memberof query.
static void Main(string[] args)
{
using (var context = new PrincipalContext(ContextType.Domain))
{
var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, "Group Name");
UserPrincipalEx qbe = new UserPrincipalEx(context);
qbe.AdvancedSearchFilter.MemberOf(group);
PrincipalSearcher searcher = new PrincipalSearcher(qbe);
var all = searcher.FindAll().OfType<UserPrincipalEx>().ToList();
foreach (var member in all)
{
Console.WriteLine(member.DisplayName);
}
}
}
public class UserPrincipalExSearchFilter : AdvancedFilters
{
public UserPrincipalExSearchFilter(Principal p) : base(p) { }
public void MemberOf(GroupPrincipal group)
{
this.AdvancedFilterSet("memberof", group.DistinguishedName, typeof(string), MatchType.Equals);
}
}
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("User")]
public class UserPrincipalEx : UserPrincipal
{
private UserPrincipalExSearchFilter searchFilter;
public UserPrincipalEx(PrincipalContext context)
: base(context)
{
}
public UserPrincipalEx(PrincipalContext context,
string samAccountName,
string password,
bool enabled)
: base(context,
samAccountName,
password,
enabled)
{
}
public new UserPrincipalExSearchFilter AdvancedSearchFilter
{
get
{
if (null == searchFilter)
searchFilter = new UserPrincipalExSearchFilter(this);
return searchFilter;
}
}
}