How do I get the current users full name from active directory

I have a text field and I want the text field to be populated with the current users full name from the active directory.
I've found code on getting the user name from active directory but I want the full name of the user.
Example: username might be CUNNINDV
but the user's full name is David Cunningham
David Cunningham is what I want to display.
Any help is appreciated!!!!!
mmtcunningham1Asked:
Who is Participating?
 
mmtcunningham1Author Commented:
I got it to work!!!
Here's the code to do it:

Dim DomainUser As String = System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
Dim ADEntry As New System.DirectoryServices.DirectoryEntry("WinNT://" & DomainUser)
Dim FullName As String = ADEntry.Properties("FullName").Value
0
 
jmwheelerCommented:
Try this:

DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + userName +",User");      
retVal = (string)userEntry.Properties["fullname"].Value;
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jmwheelerCommented:
My example above should be more clear.   You need to have your domain after winnt and then a slash and then the username.

DirectoryEntry userEntry = new DirectoryEntry("WinNT://YOURDOMAIN/USERNAME",User");      
retVal = (string)userEntry.Properties["fullname"].Value;
0
 
mmtcunningham1Author Commented:
jmwheeler:
directoryEntry not defined.  am I missing something????
0
 
jmwheelerCommented:
My refined example had an additional double-quote that wasn't needed.

You need to add a reference to System.DirectoryServices to your project/website.  Also, I think you might be using VB so the code needs converted from C#, I'm not a VB user but here is my best guess.

Dim userEntry as DirectoryEntry = new DirectoryEntry("WinNT://YOURDOMAIN/USERNAME, User")
Dim retVal as string = userEntry.Properties("fullname").Value.ToString()
0
 
jmwheelerCommented:
You also need to specify that your code should use the System.DirectoryServices namespace.  I'm not sure if VB has a "using" statement but you can always just include it right in the code.

Dim userEntry as System.DirectoryServices.DirectoryEntry = new System.DirectoryServices.DirectoryEntry("WinNT://YOURDOMAIN/USERNAME, User")

Open in new window

0
 
mmtcunningham1Author Commented:
n/a
0
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.

All Courses

From novice to tech pro — start learning today.