简体   繁体   中英

LDAP DirectorySearcher memberOf property only returns 1 group

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM