I am trying to go through all the active users in my AD and pull out various properties. I have this working accept that the memberOf property is only returning 1 of the groups a user is a part of instead of all the various groups a user is a part of. Does anyone have an idea what I am missing to have my search return all the groups?
List<ADUser> lstADUsers = new List<ADUser>();
string[] propertiesToLoad = new string[7] { "name","displayName", "telephoneNumber","description","title","department","manager","memberOf"};
string filter = "(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=32)(!userAccountControl:1.2.840.113556.1.4.803:=2)(userAccountControl:1.2.840.113556.1.4.803:=512)(samAccountType=805306368)(mail=*))";
using (var context = new DirectoryEntry("LDAP:MYLDAP"))
{
using (DirectorySearcher searcher = new DirectorySearcher(context,filter,propertiesToLoad))
{
searcher.PageSize = 15000;
searcher.SizeLimit = 15000;
foreach (SearchResult sResultSet in searcher.FindAll())
{
//main properties to get from ad
var UserModel = new ADUser();
UserModel.FullName = GetProperty(sResultSet, "name");
UserModel.DisplayName = GetProperty(sResultSet, "displayName");
UserModel.TelePhoneNumber = GetProperty(sResultSet, "telephoneNumber");
UserModel.Description = GetProperty(sResultSet, "description");
UserModel.JobTitle = GetProperty(sResultSet, "title");
UserModel.Department = GetProperty(sResultSet, "department");
UserModel.MemberOf = GetProperty(sResultSet, "memberOf");
lstADUsers.Add(UserModel);
}
}
}
Added this inside the foreach statement.
//enumerate through the memberOf property to get all the groups for the user
string memberships = string.Empty;
foreach(object memberOf in sResultSet.Properties["memberOf"])
{
memberships += memberOf.ToString() + "\n";
}