Get user names in an Active Directory group through .net

Below is the user code in the group, but it returns "CN=johnson\,Tom,OU=Users,OU=Main,DC=company,DC=com"

I just want to return the Name and Surname. How can i do this?

 DirectoryEntry ou = new DirectoryEntry(); DirectorySearcher src = new DirectorySearcher(); src.Filter = ("(&(objectClass=group)(CN=Gname))"); SearchResult res = src.FindOne(); if (res != null) { DirectoryEntry deGroup = new DirectoryEntry(res.Path); PropertyCollection pcoll = deGroup.Properties; foreach (object obj in deGroup.Properties["member"]) { ListBox1.Items.Add(obj.ToString()); } } 
+8
c # visual-studio-2010 active-directory
source share
3 answers

I prefer to use classes in System.DirectoryServices.AccountManagement:

 PrincipalContext principalContext = new PrincipalContext(ContextType.Domain); GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "GName"); 

Search the group.Members property until you reach Principal . Then extract the name as follows:

 foreach (Principal principal in group.Members) { string name = principal.Name; } 
+22
source share

Using your code, the givenName (first name) and sn (last name) properties should work.

If you use the System.DirectoryServices.AccountManagement UserPrincipal namespace (as suggested by @ russell-mcclure), you will also find GivenName and Last Name properties.

AccountManagement is very convenient if you do not need to go through a trusted forest and you need a global catalog to find the user.

+2
source share

This is the PowerShell script I made for this without using the AccountManagement classes. This should be easy enough to translate to C #:

 [void][System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices"); $groupName = "Grupo Domain"; $directoryEntry = New-Object System.DirectoryServices.DirectoryEntry; $directorySearcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry, "(&(objectClass=group)(CN=$groupName))"); [void]$directorySearcher.PropertiesToLoad.Add("objectSid"); [void]$directorySearcher.PropertiesToLoad.Add("member"); $result = $directorySearcher.FindOne(); if ($result -eq $null) { return; } # Try get the group members through the "member" property. if ($result.Properties["member"].Count -gt 0) { foreach ($member in $result.Properties["member"]) { $memberSearcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry, "(&(objectClass=*)(distinguishedName=$member))"); [void]$memberSearcher.PropertiesToLoad.Add("msDS-PrincipalName"); $memberResult = $memberSearcher.FindOne(); if ($memberResult -eq $null) { continue; } Write-Output $memberResult.Properties["msDS-PrincipalName"]; } return; } if ($result.Properties["objectSid"].Count -gt 0) { # The group might be an AD primary group. Try get the members by the PrimaryGroupID. $groupSid = New-Object System.Security.Principal.SecurityIdentifier($result.Properties["objectSid"][0], 0); # Hacky way to get only the last RID. $primaryGroupSid = $groupSid.Value.Replace($groupSid.AccountDomainSid.ToString(), [String]::Empty).TrimStart('-'); $memberSearcher = New-Object System.DirectoryServices.DirectorySearcher($directoryEntry, "(&(objectClass=*)(primaryGroupId=$primaryGroupSid))"); [void]$memberSearcher.PropertiesToLoad.Add("msDS-PrincipalName"); $memberResult = $memberSearcher.FindAll(); if ($memberResult -eq $null) { continue; } foreach ($member in $memberResult) { Write-Output $member.Properties["msDS-PrincipalName"]; } } 
0
source share

All Articles