3

I am having difficulty getting the User Name of a person logged into a machine using a windows service.

When using both System.Environment.UserName or WindowsIdentity.GetCurrent().UserName I get NTAUTHORITY\SYSTEM but when this application gets pushed I need to be able to map to the UserID of the person logged in to the system. The operating system this will be used on will be Windows XP.

Any help would be very appreciated.

Andrew Sehr
  • 342
  • 1
  • 4
  • 13
  • 2
    Which user do you want? There can be several people logged into sessions on a machine (either through fast user switching on XP, or Remote Desktop on a server). Each session can have processes running under different logins (via the Run As command). – Tim Robinson Dec 14 '10 at 18:11
  • The current logged on user. Luckily these machines can only allow for one client log-in at a time. – Andrew Sehr Dec 14 '10 at 18:16
  • Tim, you should rephrase this and post it as an answer - I suspect the OP is asking the impossible and yours is the best answer he'll get! – Dan Puzey Dec 14 '10 at 18:18
  • @Andrew it would be helpful to find out why you need this information. There may be another approach that makes more sense. – Tim Robinson Dec 14 '10 at 18:23
  • Basically I have an application that needs to check to see if the process is running and restart it if it ever is not running. But the users can nor do they need to have any interaction with that application. – Andrew Sehr Dec 14 '10 at 18:26
  • What's running in a service, and what's running in an interactive session? – Tim Robinson Dec 14 '10 at 18:48
  • The service is the watcher program to check to see if the application is running and the interactive session is just a normal WPF Windows Application – Andrew Sehr Dec 14 '10 at 19:24

3 Answers3

4

Try this:

var connectionOptions = new ConnectionOptions();
var scope = new System.Management.ManagementScope("\\\\localhost", connectionOptions);
var query = new System.Management.ObjectQuery("select * from Win32_ComputerSystem");
var searcher = new ManagementObjectSearcher(scope, query);
foreach (var row in searcher.Get()) 
{
    Console.WriteLine(row["UserName"].ToString().ToLower());
}
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    +1 this code does exactly what I want, I just wish i didn't want it – Jodrell May 17 '11 at 11:44
  • I'm also facing the same issue, @jgauffin your code will work for 64 bit OS also? – Wilz Nov 28 '12 at 17:40
  • @jgauffin thanks, One more doubt, do we get multiple user name on any case? – Wilz Nov 28 '12 at 18:03
  • if more ppl are logged on (i.e. got running applications) – jgauffin Nov 28 '12 at 19:06
  • @jgauffin: I'm getting the domain name two times for a few machines with the above code like, domain/domain/username. Is there any problem with those machines? All other machines gives it correctly domain/username. – Wilz Nov 30 '12 at 07:40
  • no. you get all processes and the username that they are running with. – jgauffin Nov 30 '12 at 08:41
  • @jgauffin, i'm using the following code, ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem"); ManagementObjectCollection collection = searcher.Get(); strUser = (string)collection.Cast().First()["UserName"]; – Wilz Nov 30 '12 at 08:56
  • But for a few systems i'm getting domain/domain/username instead of doamin/username what should i do? – Wilz Nov 30 '12 at 08:58
1

For XP only, this advice should apply: Get Window Station for a non-interactive user per process, user or session?

  • Call OpenWindowStation to get a handle to "winsta0"
  • Call GetUserObjectInformation to find out who owns winsta0

However, this approach will break when you upgrade your app to Vista, Windows 7 or above, where services run in a different session from interactive logons. You'll need to call the terminal services API to get a list of logged on users, and pick your 'interactive' one.

Community
  • 1
  • 1
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
-2

Windows service always runs under the credential supplied in the service Properties / Log On tab. This has nothing to do with the credentials of the person / people that are currently logged into Windows in their own sessions.

The value of NTAUTHORITY\SYSTEM is correct because your service is running under 'Local System account' credentials.

You cannot run windows service under the account of 'currently logged on' user - that's not what windows service is for.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • 1
    He was asking how to get the logged in username from a service, now how to run a service under the account of 'currently logged on' user – iedoc Oct 28 '13 at 19:40