Getting current user name

  • Thread starter Thread starter Steve Enzer
  • Start date Start date
S

Steve Enzer

How can I get the login name of the user currently logged into Windows as a
string?

Thanks,
Steve Enzer
 
Hi Steve,

System.Environment.UserName

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
Hi,

Imports System.Security
Imports System.Security.Principal

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
Dim myID As WindowsIdentity = WindowsIdentity.GetCurrent()
Msgbox("The Current User is : " & myID.Name)

Regards,
Cerebrus.
 
Well, in .NET 2.0 is simpler: My.User.Name, although it returns the domain
too.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
I saw your post just now and have a question, Carlos.

The definition of the Environment.Username property suggests that it
would get the name of the User who launched the current thread or
process. Now, in some cases, that username could be SYSTEM or
LOCALSERVICE or NETWORKSERVICE.

But I think that the WindowsIdentity name would always be the name of
the currently logged on user.

Please tell me if I am right (or correct me if I'm wrong ! ), and if
so, then Environment.Username would not be such a good method of
getting this information, after all.

Thanks,

Cerebrus.
 
Imports System.Security.Principal

'Get the current identity and put it into an identity object.
Dim MyIdentity As WindowsIdentity =
WindowsIdentity.GetCurrent()

'Put the previous identity into a principal object.
Dim MyPrincipal As New WindowsPrincipal(MyIdentity)

'Principal values.
Dim PrincipalName As String = MyPrincipal.Identity.Name
Dim PrincipalType As String =
MyPrincipal.Identity.AuthenticationType
Dim PrincipalAuth As String =
MyPrincipal.Identity.IsAuthenticated.ToString()



'Identity values.
Dim IdentName As String = MyIdentity.Name
Dim IdentType As String = MyIdentity.AuthenticationType
Dim IdentIsAuth As String =
MyIdentity.IsAuthenticated.ToString()
Dim ISAnon As String = MyIdentity.IsAnonymous.ToString()
Dim IsG As String = MyIdentity.IsGuest.ToString()
Dim IsSys As String = MyIdentity.IsSystem.ToString()
Dim Token As String = MyIdentity.Token.ToString()


Regards

Ernesto
 
AMDRIT said:
The My namespace doesn't exist for console applications.

Well, with conditional compilation constants it's maybe possible to add it
to console applications too, but I fear that this would be justifiable only
for using a simple method which is mainly a wrapper around .NET Framework
methods.
 
Hi,

Yes, Environment.Username makes internally a call to the Win32 API function
GetUserName and according to the docs
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getusername.asp)
it retrieves the user associated with the current thread. So, in most cases
it will do the trick but if you are calling it from a Windows service it
won´t do the trick, although it doesn´t make much sense to do it from a
Windows service (by definition)...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
 
Is there also a (simple) way to have Windows authenticate a Username /
Password combination?
I need that for an application security lock (after xxx minutes of
inactivity)
 
Back
Top