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!
test = Right(Request.ServerVariables("REMOTE_USER"), 5)
You will just need to remove the "Domain\" 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:
DirectoryEntry de = new DirectoryEntry("LDAP://...", ldapusername, password);
DirectorySearcher ds = new DirectorySearcher(de);
ds.PropertiesToLoad.Add("displayname");
ds.SearchScope = SearchScope.Subtree;
ds.Filter = "(sAMAccountName=" & username & ")";
SearchResult result = ds.FindOne();
If the user is found you can return the displayname with result.properties("displayname").value. I can probably post a complete function later.
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 = "(&(objectClass=user)(SAMAccountName=" & UserName & "))"
search.PropertiesToLoad.Add("cn")
Dim result As SearchResult = search.FindOne()
If (result Is Nothing) Then
FullName = "not found"
Else
FullName = result.Properties("cn")(0).toString()h
End If
Catch ex As Exception
End Try
Return FullName
End Function
Here is a function I use to pull the CN which you can also use instead of displayname. But you can use either.
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'
Any ideas on this?
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("REMOTE_USER"), 5)
Dim userFullName As String = UserPrincipal.Current.DisplayName
EmployeeTextBox.Text = userFullName
ReportingPeriodTextBox.Text = Now.Year
End Sub
End Class.
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
MKozlowski
Member
508 Points
591 Posts
How can I retrieve the users full name using LDAP?
Dec 14, 2011 07:54 PM|LINK
Hi,
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!
test = Right(Request.ServerVariables("REMOTE_USER"), 5)gww
Contributor
2143 Points
458 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 14, 2011 10:57 PM|LINK
You will just need to remove the "Domain\" 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:
DirectoryEntry de = new DirectoryEntry("LDAP://...", ldapusername, password); DirectorySearcher ds = new DirectorySearcher(de); ds.PropertiesToLoad.Add("displayname"); ds.SearchScope = SearchScope.Subtree; ds.Filter = "(sAMAccountName=" & username & ")"; SearchResult result = ds.FindOne();If the user is found you can return the displayname with result.properties("displayname").value. I can probably post a complete function later.
MKozlowski
Member
508 Points
591 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 14, 2011 11:01 PM|LINK
Thanks! Can you convert that to VB for me? Sorry I am kinda new to this. Thank you so much
gww
Contributor
2143 Points
458 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 12:53 AM|LINK
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 = "(&(objectClass=user)(SAMAccountName=" & UserName & "))" search.PropertiesToLoad.Add("cn") Dim result As SearchResult = search.FindOne() If (result Is Nothing) Then FullName = "not found" Else FullName = result.Properties("cn")(0).toString()h End If Catch ex As Exception End Try Return FullName End FunctionHere is a function I use to pull the CN which you can also use instead of displayname. But you can use either.
MKozlowski
Member
508 Points
591 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 12:37 PM|LINK
Thank you, I am getting a directory entry is not defined. Do I need to add a reference?
gww
Contributor
2143 Points
458 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 01:30 PM|LINK
yes add this to your page references
<% @ Import Namespace = "System.DirectoryServices" %>
MKozlowski
Member
508 Points
591 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 01:32 PM|LINK
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'
Any ideas on this?
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("REMOTE_USER"), 5) Dim userFullName As String = UserPrincipal.Current.DisplayName EmployeeTextBox.Text = userFullName ReportingPeriodTextBox.Text = Now.Year End Sub End Class.gww
Contributor
2143 Points
458 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 02:21 PM|LINK
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
MKozlowski
Member
508 Points
591 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 02:24 PM|LINK
If I do impersonation will I still need a username and password?
gww
Contributor
2143 Points
458 Posts
Re: How can I retrieve the users full name using LDAP?
Dec 15, 2011 05:18 PM|LINK
In order to make sure the code is run under that specific account, yes.
I also posted this link in your other thread, should answer some questions, http://forums.asp.net/t/897609.aspx