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

toneezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel ReynoldsSoftware Applications Developer / IntegratorCommented:
try displayname
currentUser.Properties["displayName"].Value.ToString();
0
toneezAuthor Commented:
I tried displayName as well already and it does not solve the issue unfortunately.
0
toneezAuthor Commented:
Well finally after enough tinkering around I figured this out.  I will attach the working code for anyone else who runs across this.
string userName = User.Identity.Name;
        string ldapPath = "LDAP://test.com/OU=Staff,DC=test,DC=com";
        DirectoryEntry rootEntry = new DirectoryEntry(ldapPath);
        using (DirectorySearcher ds = new DirectorySearcher(rootEntry, "(samAccountName=" + userName + ")"))
        {
            SearchResult result = ds.FindOne();
            if (result != null)
            {
                ResultPropertyValueCollection resultValues = result.Properties["givenName"];
                if (resultValues.Count > 0)
                {
                    lblUsername.Text = (string)resultValues[0];
                }
                else
                {
                    lblUsername.Text = "Unknown User";
                }
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.