Question Get DisplayName for User's Manager from distinguishedName

bjblackmore

New member
Joined
Oct 4, 2016
Messages
2
Programming Experience
Beginner
Hi,

I'm using the below code to pull a number of attributes from a users AD profile. I can get the user's manager's distinguishedName, but I want to display the displayName. How can i either pull the displayName for the manager from AD, using either another directory query, or by splitting the distinguishedName into just the name, and getting rid of the rest of the string?

I get:
manager=CN=Bloggs\, Joe,OU=IT,OU=Users,DC=domain,DC=net
I want:
manager="Joe Bloggs" or "Bloggs, Joe" (either is fine).

VB.NET:
[/COLOR][/COLOR]Private Function GetUserProperties() As ADProperties    Dim ADName As String = GetLogonName()
    Dim bSuccess As Boolean = False
    Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
    Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
    Dim waitTime As TimeSpan = New TimeSpan(0, 0, 0, 5, 0)
    dirSearcher.ClientTimeout = waitTime
    dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
    dirSearcher.PropertiesToLoad.Add("manager")
    dirSearcher.SearchScope = SearchScope.Subtree
    Try
        Dim dirResult As SearchResult = dirSearcher.FindOne()
        bSuccess = Not (dirResult Is Nothing)
        If dirResult.GetDirectoryEntry.Properties("manager").Value Is Nothing Then
            GetUserProperties.manager = "<Not Set>"
        Else
            GetUserProperties.manager = (dirResult.Properties("manager")(0).ToString())
        End If
        bSuccess = True
    Catch ex As Exception
        bSuccess = False
        MsgBox("No Connection to the domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error")
        Application.Exit()
    End Try
End Function
 
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try
        Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
        currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current
        Dim DisplayName As String = currentADUser.GivenName & " " & currentADUser.Surname
        Username.Text = currentADUser.SamAccountName
        Dim ADProp As ADProperties = GetUserProperties()
        manager.Text = ADProp.manager
    Catch ex As Exception
        MsgBox("No Connection to domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error")
        Application.Exit()
    End Try

End Sub[COLOR=#333333][COLOR=#333333]
 

bjblackmore

New member
Joined
Oct 4, 2016
Messages
2
Programming Experience
Beginner
Resolved

Not sure why this keeps being moved to the Security sub forum. it's not a security question!

Anyway, using the below code looks to have worked.

VB.NET:
Using user As New DirectoryEntry("LDAP://" & distinguishedName)
                Dim strMgrDN As String = GetADProperty(user, "manager")
                If Not strMgrDN = "" Then
                    Using Manager As New DirectoryEntry("LDAP://" & strMgrDN)
                        managerDN.Text = GetADProperty(Manager, "displayName")
                    End Using
                Else
                    managerDN.Text = "Empty"
                End If
            End Using
 
Top Bottom