Link to home
Start Free TrialLog in
Avatar of toneez
toneez

asked on

ASP.NET, C#, Active Directory - How do I display the fullname?

First off: I am using ASP.NET C# and Active Directory.

My question is... I have a login system that works off of active directory / windows auth.  When the person logs in I currently have their login name showing (i.e. Welcome smithjo) I would like to show their full name (i.e. Welcome Joe Smith!).  I have been looking all over and trying many examples of how to do this with no luck.

Can anyone help me out?  Currently I am using this code, which is throwing the error: System.NullReferenceException: Object reference not set to an instance of an object.

Code:

string ldapPath = "LDAP://test.com/OU=staff,DC=test,DC=com";
        DirectoryEntry currentUser = new DirectoryEntry(ldapPath);
 
        if (currentUser != null)
        {
            string firstName = currentUser.Properties["givenName"].Value.ToString();
            string lastName = currentUser.Properties["sn"].Value.ToString();
            lblFirstName.Text = firstName;
            lblLastName.Text = lastName;
        }
        else
        {
            lblUsername.Text = "Unknown User";
        }

Open in new window

Avatar of Daniel Reynolds
Daniel Reynolds
Flag of United States of America image

try displayname
currentUser.Properties["displayName"].Value.ToString();
Avatar of toneez
toneez

ASKER

I tried displayName as well already and it does not solve the issue unfortunately.
ASKER CERTIFIED SOLUTION
Avatar of toneez
toneez

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial