4

I tried:

var curUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

But, it is giving the output "NT AUTHORITY\SYSTEM " as the current user name after deploying the service.

Coding man
  • 957
  • 1
  • 18
  • 44
  • What do you mean by "the current user"? There can be multiple users logged in to Windows. – CodeCaster Apr 10 '15 at 09:40
  • 1
    Services often run under a different account than the *logged in* user. – Lloyd Apr 10 '15 at 09:41
  • 1
    *NT AUTHORITY\SYSTEM* is the account that your service is running under. – Bolu Apr 10 '15 at 09:41
  • 2
    Try looking here, which has a couple of solutions using WMI to get all currently logged in users when you are running a Windows service: http://stackoverflow.com/questions/5218778/how-to-get-currently-logged-username-from-windows-service-in-net – Martin Apr 10 '15 at 09:43

1 Answers1

5

If I understand your question correctly, you would like to retrieve the authenticated user on the Windows Machine from your Windows Service (is that correct)?

This will not work, because services doesn't run under a user, they are running under a given account, which can be user, but doesn't need to be.

Generally speaking, anything running as a service does not require a user to be logged in for the application(s) which depend on that service to be fully functional. Really, that's the entire bases of the Windows NT service model.

If you log on, do some work and then log off, all the processes which your logon session started will be shut down (eg: application executables, etc), but services will be unaffected.

The image below demonstrates where the running user is specified:

enter image description here

I know that this isn't an answer to your question, but I've posted this as an answer to be able to provide some description and images. I don't think there's a solution for what you're trying to achieve.

Complexity
  • 5,682
  • 6
  • 41
  • 84
  • 1
    "Not possible" is also an answer ;) – user2345998 Apr 10 '15 at 09:47
  • Actually it's possible but tricky: https://stackoverflow.com/questions/30759933/windows-service-get-username-when-user-log-on – Konrad Feb 19 '19 at 09:57
  • https://stackoverflow.com/questions/5218778/how-do-i-get-the-currently-logged-username-from-a-windows-service-in-net – Konrad Feb 19 '19 at 09:58
  • https://stackoverflow.com/questions/21676225/how-to-get-current-windows-username-from-windows-service-in-multiuser-environmen – Konrad Feb 19 '19 at 09:58