hsimah
hsimah

Reputation: 1303

Retrieve AD sAMAccountNames from memberOf property

We are using a DirectoryEntry to retrieve group membership details. The memberOf property contains the cn of the group members. However, our domain is full of groups where the cn is different from the sAMAccountName.

I need to get a list of sAMAccountNames of members of a group (including recursing through the membership of groups that are members etc). Right now I look up the memberOf property, store the cn and then do a second LDAP query to get the sAMAccountNames of the objects with those cns.

Is there a better way to accomplish this?

Upvotes: 0

Views: 223

Answers (1)

Ethan
Ethan

Reputation: 154

I'm not sure how you are getting the CN but when I need the SamAccountNames from a group I usually write something like this,

var samNames = new List<string>();
using (var group = GroupPrincipal.FindByIndentity(principalContext, "GroupName"))
{
     if (group != null)
     {
         var users = group.GetMembers(true);
         foreach (UserPrincipal user in users)
         {
             samNames.add(user.SamAccountName);
         }
     }
}

Upvotes: 1

Related Questions