2

How do I get the current logged in Windows user? my problem: i'm running a process with administrator privileges and all these:

Console.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
Console.WriteLine(Environment.UserName);
Console.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().User); //GUID
Console.WriteLine(Environment.GetEnvironmentVariable("USERNAME"));

...tries give me back the current user who runs the process, in my case Administrator - but i'd like to have the current user who is logged in.

Any ideas or suggestions?

Markus
  • 310
  • 1
  • 3
  • 17
  • Possible duplicate of https://stackoverflow.com/questions/10666685/c-sharp-net-how-to-detect-if-a-process-is-running-for-the-current-logged-in-use?rq=1 – Uwe Keim Feb 01 '17 at 07:28
  • @UweKeim Not sure how that's a duplicate –  Feb 01 '17 at 07:51
  • _"i'm running a process with administrator privileges "_ - do you mean the process is running _elevated_? What OS is this because when I add an **app.manifest** and include `` for my test app, it runs elevated and yet still displays _my name_ correctly and not "administrator" or similar –  Feb 01 '17 at 08:09
  • 2
    @MickyD - that's because you have administrator rights. If you were running as a non-admin, then elevation would also prompt you for an alternate username and password. You can see that sort of thing discussed in the Raymond Chen article I linked at the bottom of my answer. – Damien_The_Unbeliever Feb 01 '17 at 08:12
  • @Damien_The_Unbeliever ahhhh thanks Damien :) –  Feb 01 '17 at 08:15
  • Related, different language: http://stackoverflow.com/q/28672303/886887 – Harry Johnston Feb 02 '17 at 02:19

2 Answers2

4

I found this method a long time ago. I am using the WMI query

ManagementObjectSearcher searcher = 
     new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");

ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
Bundeskanzler.
  • 90
  • 1
  • 11
  • I never used this method for 2 users. But the question is not for 2 users? – Bundeskanzler. Feb 01 '17 at 08:01
  • 1
    Perhaps, but it's not a fool-proof method. _"[In a terminal services session, UserName returns the name of the user that is logged on to the console—not the user logged on during the terminal service session](https://msdn.microsoft.com/en-us/library/aa394102(v=vs.85).aspx)"_ –  Feb 01 '17 at 08:05
  • Oh. For my project it worked, but if that's right: I'm wrong. Sorry – Bundeskanzler. Feb 01 '17 at 08:08
  • if current user is not an admin, which run the application, the collection.Cast() include 1 empty item. So, this solution doesn't work in my case – Vitaliy Jun 04 '19 at 08:14
2

The correct way, I believe, would be to execute WTSQuerySessionInformation, something along the lines of:

WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION,
                           WTSUserName,buffer, out byteCount);

PInvoke page for this function.


Tangentially related, but may be of interest - How can I launch an unelevated process from my elevated process and vice versa?

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448