Home All Groups Group Topic Archive Search About

Getting current user name

Author
16 Feb 2006 4:09 PM
Steve Enzer
How can I get the login name of the user currently logged into Windows as a
string?

Thanks,
Steve Enzer

Author
16 Feb 2006 4:15 PM
Carlos J. Quintero [VB MVP]
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


Show quoteHide quote
"Steve Enzer" <nospam@nospam.com> escribió en el mensaje
news:e1L3XNxMGHA.1124@TK2MSFTNGP10.phx.gbl...
> How can I get the login name of the user currently logged into Windows as
> a string?
>
> Thanks,
> Steve Enzer
>
Author
16 Feb 2006 4:27 PM
Al Reid
"Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in message news:OpKmARxMGHA.3720@TK2MSFTNGP10.phx.gbl...
> Hi Steve,
>
> System.Environment.UserName

How simple.  Ive been using
System.Security.Principal.WindowsIdentity.GetCurrent().Name



--

Al Reid


Show quoteHide quote
>
> --
>
> Best regards,
>
> Carlos J. Quintero
>
Author
16 Feb 2006 4:33 PM
Carlos J. Quintero [VB MVP]
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



Show quoteHide quote
"Al Reid" <arei***@reidDASHhome.com> escribió en el mensaje
news:OvpF1XxMGHA.740@TK2MSFTNGP12.phx.gbl...
> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
> message news:OpKmARxMGHA.3720@TK2MSFTNGP10.phx.gbl...
>> Hi Steve,
>>
>> System.Environment.UserName
>
> How simple.  Ive been using
> System.Security.Principal.WindowsIdentity.GetCurrent().Name
>
Author
16 Feb 2006 5:56 PM
Joe Sutphin
FYI, the My.User.Name functionality does not work for a console app.

Show quoteHide quote
"Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in message
news:%23CKTJbxMGHA.2696@TK2MSFTNGP14.phx.gbl...
> 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
>
>
>
> "Al Reid" <arei***@reidDASHhome.com> escribió en el mensaje
> news:OvpF1XxMGHA.740@TK2MSFTNGP12.phx.gbl...
>> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
>> message news:OpKmARxMGHA.3720@TK2MSFTNGP10.phx.gbl...
>>> Hi Steve,
>>>
>>> System.Environment.UserName
>>
>> How simple.  Ive been using
>> System.Security.Principal.WindowsIdentity.GetCurrent().Name
>>
>
Author
16 Feb 2006 10:17 PM
AMDRIT
The My namespace doesn't exist for console applications.

Show quoteHide quote
"Joe Sutphin" <joesutp***@earthlink.net> wrote in message
news:uZjoKJyMGHA.420@tk2msftngp13.phx.gbl...
> FYI, the My.User.Name functionality does not work for a console app.
>
> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
> message news:%23CKTJbxMGHA.2696@TK2MSFTNGP14.phx.gbl...
>> 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
>>
>>
>>
>> "Al Reid" <arei***@reidDASHhome.com> escribió en el mensaje
>> news:OvpF1XxMGHA.740@TK2MSFTNGP12.phx.gbl...
>>> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
>>> message news:OpKmARxMGHA.3720@TK2MSFTNGP10.phx.gbl...
>>>> Hi Steve,
>>>>
>>>> System.Environment.UserName
>>>
>>> How simple.  Ive been using
>>> System.Security.Principal.WindowsIdentity.GetCurrent().Name
>>>
>>
>
>
Author
16 Feb 2006 11:44 PM
Herfried K. Wagner [MVP]
"AMDRIT" <amd***@hotmail.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
20 Feb 2006 4:36 PM
Joe Sutphin
No, duh !

Show quoteHide quote
"AMDRIT" <amd***@hotmail.com> wrote in message
news:ePNxva0MGHA.1532@TK2MSFTNGP12.phx.gbl...
> The My namespace doesn't exist for console applications.
>
> "Joe Sutphin" <joesutp***@earthlink.net> wrote in message
> news:uZjoKJyMGHA.420@tk2msftngp13.phx.gbl...
>> FYI, the My.User.Name functionality does not work for a console app.
>>
>> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
>> message news:%23CKTJbxMGHA.2696@TK2MSFTNGP14.phx.gbl...
>>> 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
>>>
>>>
>>>
>>> "Al Reid" <arei***@reidDASHhome.com> escribió en el mensaje
>>> news:OvpF1XxMGHA.740@TK2MSFTNGP12.phx.gbl...
>>>> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
>>>> message news:OpKmARxMGHA.3720@TK2MSFTNGP10.phx.gbl...
>>>>> Hi Steve,
>>>>>
>>>>> System.Environment.UserName
>>>>
>>>> How simple.  Ive been using
>>>> System.Security.Principal.WindowsIdentity.GetCurrent().Name
>>>>
>>>
>>
>>
>
>
Author
17 Feb 2006 10:53 AM
Martin
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)

Show quoteHide quote
"Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in message
news:%23CKTJbxMGHA.2696@TK2MSFTNGP14.phx.gbl...
> 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
>
>
>
> "Al Reid" <arei***@reidDASHhome.com> escribió en el mensaje
> news:OvpF1XxMGHA.740@TK2MSFTNGP12.phx.gbl...
>> "Carlos J. Quintero [VB MVP]" <carlosq@NOSPAMsogecable.com> wrote in
>> message news:OpKmARxMGHA.3720@TK2MSFTNGP10.phx.gbl...
>>> Hi Steve,
>>>
>>> System.Environment.UserName
>>
>> How simple.  Ive been using
>> System.Security.Principal.WindowsIdentity.GetCurrent().Name
>>
>
Author
16 Feb 2006 4:33 PM
Cerebrus99
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.
Author
16 Feb 2006 5:01 PM
Cerebrus99
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.
Author
17 Feb 2006 9:45 AM
Carlos J. Quintero [VB MVP]
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


Show quoteHide quote
"Cerebrus99" <zorg***@sify.com> escribió en el mensaje
news:1140108221.805677.95260@g43g2000cwa.googlegroups.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.
>
Author
16 Feb 2006 7:47 PM
ernesth
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
Author
16 Feb 2006 8:08 PM
Steve Enzer
Thanks for all the suggestions!

Steve