0

i use the below code to get logged in user name.

private string GetLoggedInUser()
{
    string userName = "";
    if (System.Security.Principal.WindowsIdentity.GetCurrent() != null)
    {
        userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    }
    return userName;
}

just do not understand why it is not giving a logged in user name rather it is return data in this format NT AUTHORITY\SYSTEM but i want to get user name.

i try to run my service as Local Service and local system account but in every case i am getting this string NT AUTHORITY\SYSTEM instead of logged in user name.

so please guide me what to change in code. thanks

Mou
  • 15,673
  • 43
  • 156
  • 275
  • 2
    Because Local Service and Local System account both run processes under the NT Authority\System user. You are getting the current user (aka the user RUNNING the process), not the user that is logged into the interactive session. Are you wanting to get the interactive user? Or the user running the process? – Ron Beyer Apr 21 '15 at 14:08
  • are you also familiar with `WMI` you may want to do a google search on how to use that check out this link it may or may not help https://msdn.microsoft.com/en-us/library/bb546085%28v=vs.110%29.aspx – MethodMan Apr 21 '15 at 14:11
  • i want user name that is logged into the interactive session – Mou Apr 21 '15 at 14:12
  • Environment.UserName give what kind of user name – Mou Apr 21 '15 at 14:12
  • @Mou the current user name, has nothing to do with the service user – Xi Sigma Apr 21 '15 at 14:16
  • i like to know is it possible to get current logged in user name from windows service? Say for example Smith logged in and i want windows service should return the name Smith....is it possible and how ? – Mou Apr 21 '15 at 18:57

0 Answers0