I am writing an application that will run on windows server. The scenario is that user will login using normal domain account and application will run using administrator account. I need to get the user currently logged in to windows to do some processing.
when I get user name using following method it gives me the admin account used to run the exe and not the account logged in to windows current session.
string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Then I tried following method to get the logged in user
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username = (string)collection.Cast<ManagementBaseObject>().First()["UserName"];
This code is working fine on windows 10 and giving me the user currently logged in to windows. But on windows server 2012 it is returning empty string.
How can I get the logged in user when running the exe on windows server?? I dont want to know how many sessions are running on windows server. i want to know the current logged in user.