Re: Get current logged-in user
- From: "Jani Järvinen [MVP]" <janij@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 28 Oct 2006 12:22:02 +0300
Lucas,
User A is logged in to a windows 2000 terminal. He runs an application
which
runs under the credentials of a different user. If I try to see the
current
user throught the application I can only see the user that started the
thread. How can I see the user that is logged in??
By "terminal", do you mean a physical PC, a user running cmd.exe (the "DOS
prompt"), or a server with Terminal Services and/or Citrix MetaFrame
running? Because Windows operating systems since NT4 can support multiple
users at the same time, there can be multiple explorer.exe's running at the
same time. Which of these users do you want? Also, does the application that
you refer to impersonate another user or not?
See this blog entry for discussion about the problem:
http://blogs.msdn.com/oldnewthing/archive/2006/08/22/712677.aspx
Now, my guess is that you are referring to the person physically attached to
the PC and his/her username, and in this case you could use the Terminal
Services (Win32) API functions WTSGetActiveConsoleSessionId and
WTSQuerySessionInformation:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/wtsquerysessioninformation.asp
First, you would call WTSGetActiveConsoleSessionId to get a session
identifier to the active console session (if any) and then given the session
ID, you would call WTSQuerySessionInformation with the WTSUserName
enumeration. This might be the information that you are looking for, though
I'm not exactly sure. Of course, you would need to use P/Invoke from .NET
code to get to these unmanaged API functions.
As for reference to others, to easily get the name of the user that is
running your application, you can use these properties in .NET:
- Environment.UserName
- System.Windows.Forms.SystemInformation.UserName
- or System.Security.Principal.WindowsIdentity.GetCurrent().Name
The last one returns the domain/computer name along with the username; the
two former ones only return the username.
Hope this helps!
--
Regards,
Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@xxxxxxxxxxxxxxxxxxxxxx
http://www.saunalahti.fi/janij/
.
- Follow-Ups:
- Re: Get current logged-in user
- From: Lattis
- Re: Get current logged-in user
- Prev by Date: Re: form design question
- Next by Date: how much time do you spend filling in exam survey?
- Previous by thread: Re: Printform issues
- Next by thread: Re: Get current logged-in user
- Index(es):
Relevant Pages
|