11

I'm trying to get the user name of the current user. When I log in as Johnny Smith and run my application without administrator privileges it will return me the correct user name, Johnny Smith. But the problem is that when I right click and choose "Run as Administrator", Windows will prompt me with a login screen for the administrator and after login my application returns user name admin, not the user which is logged in currently.

I have tried:

strUserLabel.Text = Environment.UserName

Also

Dim WSHNetwork = CreateObject("WScript.Network")
Dim strUser = ""

While strUser = ""
    strUser = WSHNetwork.Username
End While

strUserLabel.Text = strUser

Both return me the administrator user name when prompted as administrator.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mario LIPCIK
  • 457
  • 2
  • 9
  • 24
  • ok but I want to remain username of user not of admin. – Mario LIPCIK Jun 23 '14 at 11:03
  • 3
    `strUserLabel.Text = Environment.UserName` this should work because am using it many forms – Vivek S. Jun 23 '14 at 11:14
  • http://stackoverflow.com/a/14518508/1271037 – dovid Jun 23 '14 at 11:16
  • 1
    @MarioLIPCIK try `strUserLabel.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name` and comment what you are getting – Vivek S. Jun 23 '14 at 11:33
  • @MarioLIPCIK `strUserLabel.Text =System.Environment.UserName` – Vivek S. Jun 23 '14 at 11:41
  • @hector `strUserLabel.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name` returned me 'NetworkName\Username' and `strUserLabel.Text = Environment.UserName` returned just user name but it still was changing befor when I ran it as Admin – Mario LIPCIK Jun 23 '14 at 12:22

6 Answers6

11

In the MSDN documentation, I discovered they changed the definition of property Environment.UserName.

Before .NET 3

Gets the user name of the person who started the current thread.

Starting from version 3

Gets the user name of the person who is currently logged on to the Windows operating system

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dovid
  • 6,354
  • 3
  • 33
  • 73
  • 1
    i think @Mario LIPCIK should store his `user` to a `string` before `run as admin` for his application so that he can get the actual user from the stored variable – Vivek S. Jun 23 '14 at 11:39
  • 4
    @hector - you can't do that. If you right click and run as admin then the whole process is elevated from the start and is running under a different user – Matt Wilko Jun 23 '14 at 11:43
  • @hector yes my aplication works like that now, but I have several scripts in it which requre admin rights... so when I run my script wil will ask me for every single script to enter password. That is the reason I need my application to run as admin but get corent logged on user – Mario LIPCIK Jun 23 '14 at 13:23
6

I think the accepted answer above is a VERY resource intensive way to find a username. It has nested loops with hundreds of items. In my 8GP RAM PC this takes 2+ seconds!

How about:

  • Username: SystemInformation.Username, and
  • DomainName: Environment.UserDomainName

Tested in VS2017

Hannington Mambo
  • 998
  • 2
  • 13
  • 28
3

I have figured it out. I used this function which will determine which process which the user is using. In my code I defined that look for username of the explorer.exe process.

Function GetUserName() As String

    Dim selectQuery As Management.SelectQuery = New Management.SelectQuery("Win32_Process")
    Dim searcher As Management.ManagementObjectSearcher = New Management.ManagementObjectSearcher(selectQuery)
    Dim y As System.Management.ManagementObjectCollection
    y = searcher.Get

    For Each proc As Management.ManagementObject In y
        Dim s(1) As String
        proc.InvokeMethod("GetOwner", CType(s, Object()))
        Dim n As String = proc("Name").ToString()
        If n = "explorer.exe" Then
            Return s(0)
        End If
    Next
End Function

Index of 0 will return username

Index of 1 will return domain name of user

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mario LIPCIK
  • 457
  • 2
  • 9
  • 24
0

SystemInformation.Username doesn't work for certain applications. In my case, code is being run as System but explorer.exe is being run as Daniel. SystemInformation.Username reports System.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Dan
  • 1
0

if using Identity Dim UserEmail As String = Context.User.Identity.Name.ToString

Hamada
  • 11
  • 2
0

Make sure you Imports System then you can use Environment.UserName

PDUB
  • 1