Using LDAP Provider to retrieve First Name and Last Name

  • Thread starter Thread starter Prasad Karunakaran
  • Start date Start date
P

Prasad Karunakaran

I am using the C# DirectoryEntry class to retrieve the Properties of
an user object in the Active Directory. I need to get the First Name
and Last Name as properties. I know it is not supported with the ADSI
NT Provider and only supported in the LDAP Provider.

So given an UserId (UID) how can I read the First Name and Last Name
using LDAP Provider. If anybody can help me with a C# sample code it
would of great help.

Thanks in advance.

regards,

Prasad
 
I am using the C# DirectoryEntry class to retrieve the Properties of
an user object in the Active Directory. I need to get the First Name
and Last Name as properties. I know it is not supported with the ADSI
NT Provider and only supported in the LDAP Provider.

You might want to check out my ADSI browser "BeaverTail" - it will
show you your AD objects, and their properties, with the corresponding
LDAP attribute names.

http://adsi.mvps.org/adsi/CSharp/beavertail.html

As for first and last name, you'd have to use the LDAP properties
"givenName" (first name), and "sn" (for surname = last name) to grab
those values:

DirectoryEntry deUser = new
DirectoryEntry("LDAP://cn=YourUser,cn=Users,dc=YourCompany,dc=com");

if(deUser != null)
{
string sFirstName =
deUser.Properties["givenName"].Value.ToString();

string sLastName = deUser.Propeties["sn"].Value.ToString();
}

Mind you - you might want to wrap those calls into some error handling
code, since if your user for some reason doesn't have a first name or
last name attribute value set, those calls will crash (since the
deUser.Properties["sn"] will be null, if the "sn" attribute is not
set).

HTH
Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top