Menu
Home
Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Home
Forums
VB.NET
Security
Get DisplayName for User's Manager from distinguishedName
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="bjblackmore" data-source="post: 177142" data-attributes="member: 52956"><p><span style="color: #333333"><span style="color: #333333">Hi,</span></span></p><p></p><p><span style="color: #333333"><span style="color: #333333">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?</span></span></p><p></p><p><span style="color: #333333"><span style="color: #333333">I get: </span></span></p><p><span style="color: #333333"><span style="color: #333333">manager=CN=Bloggs\, Joe,OU=IT,OU=Users,DC=domain,DC=net</span></span></p><p><span style="color: #333333"><span style="color: #333333">I want:</span></span></p><p><span style="color: #333333"><span style="color: #333333">manager="Joe Bloggs" or "Bloggs, Joe" (either is fine).</span></span></p><p><span style="color: #333333"><span style="color: #333333"></span></span></p><p><span style="color: #333333"><span style="color: #333333">[CODE][/COLOR][/COLOR]Private Function GetUserProperties() As ADProperties Dim ADName As String = GetLogonName()</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim bSuccess As Boolean = False</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim dirEntry As DirectoryEntry = GetDirectoryEntry()</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim waitTime As TimeSpan = New TimeSpan(0, 0, 0, 5, 0)</span></span></p><p><span style="color: #333333"><span style="color: #333333"> dirSearcher.ClientTimeout = waitTime</span></span></p><p><span style="color: #333333"><span style="color: #333333"> dirSearcher.Filter = ("(samAccountName=" & ADName & ")")</span></span></p><p><span style="color: #333333"><span style="color: #333333"> dirSearcher.PropertiesToLoad.Add("manager")</span></span></p><p><span style="color: #333333"><span style="color: #333333"> dirSearcher.SearchScope = SearchScope.Subtree</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Try</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim dirResult As SearchResult = dirSearcher.FindOne()</span></span></p><p><span style="color: #333333"><span style="color: #333333"> bSuccess = Not (dirResult Is Nothing)</span></span></p><p><span style="color: #333333"><span style="color: #333333"> If dirResult.GetDirectoryEntry.Properties("manager").Value Is Nothing Then</span></span></p><p><span style="color: #333333"><span style="color: #333333"> GetUserProperties.manager = "<Not Set>"</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Else</span></span></p><p><span style="color: #333333"><span style="color: #333333"> GetUserProperties.manager = (dirResult.Properties("manager")(0).ToString())</span></span></p><p><span style="color: #333333"><span style="color: #333333"> End If</span></span></p><p><span style="color: #333333"><span style="color: #333333"> bSuccess = True</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Catch ex As Exception</span></span></p><p><span style="color: #333333"><span style="color: #333333"> bSuccess = False</span></span></p><p><span style="color: #333333"><span style="color: #333333"> MsgBox("No Connection to the domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error")</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Application.Exit()</span></span></p><p><span style="color: #333333"><span style="color: #333333"> End Try</span></span></p><p><span style="color: #333333"><span style="color: #333333">End Function</span></span></p><p><span style="color: #333333"><span style="color: #333333"></span></span></p><p><span style="color: #333333"><span style="color: #333333">Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Try</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal</span></span></p><p><span style="color: #333333"><span style="color: #333333"> currentADUser = System.DirectoryServices.AccountManagement.UserPrincipal.Current</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim DisplayName As String = currentADUser.GivenName & " " & currentADUser.Surname</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Username.Text = currentADUser.SamAccountName</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Dim ADProp As ADProperties = GetUserProperties()</span></span></p><p><span style="color: #333333"><span style="color: #333333"> manager.Text = ADProp.manager</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Catch ex As Exception</span></span></p><p><span style="color: #333333"><span style="color: #333333"> MsgBox("No Connection to domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error")</span></span></p><p><span style="color: #333333"><span style="color: #333333"> Application.Exit()</span></span></p><p><span style="color: #333333"><span style="color: #333333"> End Try</span></span></p><p><span style="color: #333333"><span style="color: #333333"></span></span></p><p><span style="color: #333333"><span style="color: #333333">End Sub[COLOR=#333333][COLOR=#333333][/CODE]</span></span></p><p><span style="color: #333333"><span style="color: #333333"></span></span></p></blockquote><p></p>
[QUOTE="bjblackmore, post: 177142, member: 52956"] [COLOR=#333333][COLOR=#333333]Hi,[/COLOR][/COLOR] [COLOR=#333333][COLOR=#333333]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?[/COLOR][/COLOR] [COLOR=#333333][COLOR=#333333]I get: [/COLOR][/COLOR] [COLOR=#333333][COLOR=#333333]manager=CN=Bloggs\, Joe,OU=IT,OU=Users,DC=domain,DC=net[/COLOR][/COLOR] [COLOR=#333333][COLOR=#333333]I want:[/COLOR][/COLOR] [COLOR=#333333][COLOR=#333333]manager="Joe Bloggs" or "Bloggs, Joe" (either is fine). [CODE][/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][/CODE] [/COLOR][/COLOR] [/QUOTE]
Insert quotes…
Verification
Post reply
Home
Forums
VB.NET
Security
Get DisplayName for User's Manager from distinguishedName
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom