Retrieveing full Username from Enviroment?

L

Lars Netzel

Hi

If I use the code:

MsgBox(Environment.UserName)

.... I get the username you log into windows with

But how do I retrieve that Full Name of that useraccount?

Best Regards/
Lars Netzel
 
S

ScottL

If you are in a domain with Active Directory you could query Active
directory to get the information.

Something like:

'Please note that this code is rough and may need a bit
of tweaking.

'Hard Coding for simplicity of example
Dim strcon as string = "LDAP://mydomain.com"
Dim strID as string = "MYID"
Dim strpwd as string = "MYPWD"
Dim strUserName as string =
user.identity.name.Substring(user.identity.name.IndexOf("\") + 1)


Dim Root As System.DirectoryServices.DirectoryEntry =
New System.DirectoryServices.DirectoryEntry(strcon, strID, strpwd)
Dim ds As System.DirectoryServices.DirectorySearcher =
New System.DirectoryServices.DirectorySearcher(Root)
Dim searchresult As DirectoryServices.SearchResult

'set the AD to filter to only the username we are
looking for, then load the ad record
ds.Filter = "SAMAccountName=" & strUserName
ds.PropertiesToLoad.Add("cn")
For Each searchresult In ds.FindAll()
strADReturn =
searchresult.GetDirectoryEntry.Name
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top