Re: Get current logged-in user



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/


.



Relevant Pages

  • Re: os users, sessions, and connections.
    ... From what I've read a session is a logged connection. ... do the following (paddr is the address of the process ... select sid, serial#, paddr, username from v$session ...
    (comp.databases.oracle.server)
  • Re: Get current logged-in user
    ... WTSClientBuildNumber ... Public Shared Function WTSQuerySessionInformation(ByVal hServer As ... UserName = ' This is were I dont know what to do! ... you would call WTSGetActiveConsoleSessionId to get a session ...
    (microsoft.public.dotnet.general)
  • Re: session value lost
    ... >into as sessionvariable (username). ... So I redirect to my search.aspx. ... >I save the recordid to the session and redirect to work.aspx. ... >new sqlstring I fetch the data from the sqlserver. ...
    (microsoft.public.dotnet.framework.aspnet)
  • loosing session value - sometimes
    ... into as sessionvariable (username). ... I save the recordid to the session and redirect to work.aspx. ... and I will change the username from manuel login to ...
    (microsoft.public.dotnet.security)
  • Re: question about more secure authentication
    ... when the username and password match the entries in the database, ... now, i've been reading about SESSION hijacking, etc and i'd like to ... make this login much more secure. ... more secure, use https: protocol. ...
    (alt.php)