3

In C# How to get the currently logged on windows user if the application is executed by differnt user using Run as?

WindowsIdentity.GetCurrent(); always gives the Run as user or impersonated user. I just want the windows logged on user name and user account type.

Saran
  • 835
  • 3
  • 11
  • 31
  • I've searched some time back something similar regarding UAC. The result was that this does not seem to be possible. – Uwe Keim Aug 14 '12 at 11:58
  • "the currently logged on windows user" - doesn't exist. There can be multiple users logged on to a windows machine. Fast user switching. Terminal Services. Media Extender. All of these things have existed for quite some time. – Damien_The_Unbeliever Aug 14 '12 at 11:59
  • Raymond Chen covered this on [a blog post back in 2006](http://blogs.msdn.com/b/oldnewthing/archive/2006/08/22/712677.aspx). As he said, no such thing as just one, so we need to know *what* purpose you're trying to put this information to. – Damien_The_Unbeliever Aug 14 '12 at 12:05
  • if you are running as a different user; then the current user is that different user – Dave Lawrence Aug 14 '12 at 12:07

3 Answers3

1

As far as I know, there is only one special case in which this can be accomplished: the operating system must be non-server (Windows XP/Vista/7) and fast user switching is disabled.

In this case, the "currently logged on Windows user" is simply the owner of the explorer.exe process.

If that is suitable I will put up some sample code later to show how to do this.

How do I determine the owner of a process in C#? (Has code sample)

How do you get the UserName of the owner of a process? (Links to some more advanced techniques)

Some short code coming soon.

Community
  • 1
  • 1
nicholas
  • 2,969
  • 20
  • 39
1

Try to use WMI to get logon user sessions: "Select * from Win32_LogonSession Where LogonType = 2"

Oleg Ignatov
  • 877
  • 2
  • 8
  • 22
  • That's a good one too. Hers's another SO article with details on it: http://stackoverflow.com/questions/994652/c-sharp-list-of-logon-sessions . According to the comments this one even works with Fast User Switching – nicholas Aug 14 '12 at 12:48
  • That article actually recommends using `SELECT UserName FROM Win32_ComputerSystem` instead of `Win32_LogonSession`. `Win32_ComputerSystem` is what I am using. – Krondorian Oct 02 '13 at 15:51
0

I wrote a class called Unimpersonator a few years ago, to solve the problem of ASP.NET server-side code running under an impersonation context and not being able to access network resources.

What it does is capture the current impersonation context, revert all impersonation so you return to the logged-on account, and then re-impersonates when the object is disposed:

// Running as impersonated account.
using (new Unimpersonator())
{
   // Running as logged-in-account
}
// Running as impersonated account again.
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
  • I have tried to use this to run an application as another user and when I call Environment.Username within the Unimpersonator using block, it still returns the username of the "run as" user and not the current Windows user – Guru Josh Jan 14 '22 at 00:43