Reputation: 935
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.
Upvotes: 3
Views: 2484
Reputation: 14700
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.
Upvotes: 0
Reputation: 865
Try to use WMI to get logon user sessions: "Select * from Win32_LogonSession Where LogonType = 2"
Upvotes: 1
Reputation: 3047
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.
Upvotes: 1