I'm working in .net 2.0 and need to retrieve all the users of a given AD group. I have the following method that does return all the members of the group, but it does not return users that have the passed group as their primary group. What do I need to do to get those users included as well?
/// <summary>
/// Gets the group child users.
/// </summary>
/// <param name="parentGroup">The parent group.</param>
/// <returns></returns>
public List<ADUser> GetGroupChildUsers(ADGroup parentGroup)
{
List<ADUser> list = new List<ADUser>();
DirectoryEntry entry = GetDirectoryEntry(LdapBaseString);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.Filter = string.Format("(&(objectCategory=person)(memberOf={0}))", parentGroup.DN);
searcher.PropertiesToLoad.Add("objectGUID");
searcher.SizeLimit = MaxReturnCount;
SearchResultCollection results = searcher.FindAll();
foreach (SearchResult result in results) {
Guid guid = new Guid((byte[])result.Properties["objectGUID"][0]);
list.Add(GetUserByGuid(guid));
}
if (list.Count <= 0) {
return null;
} else {
return list;
}
}