How can I retrieve the users full name using LDAP?http://forums.asp.net/t/1749725.aspx/1?How+can+I+retrieve+the+users+full+name+using+LDAP+Thu, 15 Dec 2011 17:18:44 -050017497254731812http://forums.asp.net/p/1749725/4731812.aspx/1?How+can+I+retrieve+the+users+full+name+using+LDAP+How can I retrieve the users full name using LDAP? <p>Hi,</p> <p>I have an asp.net page in VB that I am trying to populate the user field with the full name. The code below gets me the domain/username but I need the actual name of the person. How would I get that using LDAP and populate a text box? Thanks!</p> <pre class="prettyprint">test = Right(Request.ServerVariables(&quot;REMOTE_USER&quot;), 5)</pre> 2011-12-14T19:54:10-05:004731947http://forums.asp.net/p/1749725/4731947.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>You will just need to remove the &quot;Domain\&quot; part from the REMOTE_USER to get the person's username by itself then use it to search ldap using the samaccountname and return the displayname. For example:</p> <pre class="prettyprint">DirectoryEntry de = new DirectoryEntry(&quot;LDAP://...&quot;, ldapusername, password); DirectorySearcher ds = new DirectorySearcher(de); ds.PropertiesToLoad.Add(&quot;displayname&quot;); ds.SearchScope = SearchScope.Subtree; ds.Filter = &quot;(sAMAccountName=&quot; &amp; username &amp; &quot;)&quot;; SearchResult result = ds.FindOne();</pre> <p>If the user is found you can return the displayname with result.properties(&quot;displayname&quot;).value. I can probably post a complete function later.</p> 2011-12-14T22:57:42-05:004731950http://forums.asp.net/p/1749725/4731950.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>Thanks! Can you convert that to VB for me? Sorry I am kinda new to this. Thank you so much</p> 2011-12-14T23:01:23-05:004732015http://forums.asp.net/p/1749725/4732015.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <pre class="prettyprint">Dim entry As DirectoryEntry = New DirectoryEntry(LDAPstr, LDAPuser, LDAPpass) Dim FullName as string Try Dim obj As Object = entry.NativeObject Dim search As DirectorySearcher = New DirectorySearcher(entry) search.Filter = &quot;(&amp;(objectClass=user)(SAMAccountName=&quot; &amp; UserName &amp; &quot;))&quot; search.PropertiesToLoad.Add(&quot;cn&quot;) Dim result As SearchResult = search.FindOne() If (result Is Nothing) Then FullName = &quot;not found&quot; Else FullName = result.Properties(&quot;cn&quot;)(0).toString()h End If Catch ex As Exception End Try Return FullName End Function</pre> <p>Here is a function I use to pull the CN which you can also use instead of displayname. But you can use either.</p> <pre class="prettyprint">&nbsp;</pre> 2011-12-15T00:53:31-05:004732939http://forums.asp.net/p/1749725/4732939.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>Thank you, I am getting a directory entry is not defined. Do I need to add a reference?</p> 2011-12-15T12:37:48-05:004733034http://forums.asp.net/p/1749725/4733034.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>yes add this to your page references</p> <p>&lt;% @ Import Namespace = &quot;System.DirectoryServices&quot; %&gt;</p> 2011-12-15T13:30:36-05:004733037http://forums.asp.net/p/1749725/4733037.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>Thank you for your help, I have it working on my computer but if I try externally I am getting the error Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'</p> <p>Any ideas on this?</p> <pre class="prettyprint">Imports System.Security Imports System.Security.Principal.WindowsIdentity Imports Microsoft.Win32 Imports System.DirectoryServices.AccountManagement Imports System.DirectoryServices.AccountManagement.UserPrincipal Imports System.Web.Hosting Partial Class _Default Inherits System.Web.UI.Page Dim UserAccount As String Dim test As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load test = Right(Request.ServerVariables(&quot;REMOTE_USER&quot;), 5) Dim userFullName As String = UserPrincipal.Current.DisplayName EmployeeTextBox.Text = userFullName ReportingPeriodTextBox.Text = Now.Year End Sub End Class.</pre> 2011-12-15T13:32:30-05:004733137http://forums.asp.net/p/1749725/4733137.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>You probably need to make sure the code is running under permissions that can access the ldap. Either with impersonnation in the web.config or as I have in my function, passing the username and password with the directoryentry</p> 2011-12-15T14:21:06-05:004733148http://forums.asp.net/p/1749725/4733148.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>If I do impersonation will I still need a username and password?</p> 2011-12-15T14:24:48-05:004733456http://forums.asp.net/p/1749725/4733456.aspx/1?Re+How+can+I+retrieve+the+users+full+name+using+LDAP+Re: How can I retrieve the users full name using LDAP? <p>In order to make sure the code is run under that specific account, yes.</p> <p>I also posted this link in your other thread, should answer some questions, <a href="../../t/897609.aspx"> http://forums.asp.net/t/897609.aspx</a></p> 2011-12-15T17:18:44-05:00