Retrieveing full Username from Enviroment?

  • Thread starter Thread starter Lars Netzel
  • Start date Start date
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
 
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
 
Back
Top