currently logged on username from within service

G

Guest

I'm working on a system service application that is supposed to report various information about the computer it is running on back to a central server. One of the things I need to know is the username of the person who is currently logged on to the actual computer. I've tried using
Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
However, since the service is owned by "System" I get "NT_AUTHORITY\SYSTEM" as a result. So how do I get the username of the person who is logged on instead of the name of the owner of the current process?

Thanks for any help,
Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Chris,

I think you might want to re-think what you are trying to do. It is
actually possible to have multiple logon sessions on the computer at the
same time (a process might run another process as a different user, causing
another logon session, for example).

In order to get the information about these logon sessions, your easiest
solution is to go through the System.Management namespace, querying for all
instances of the Win32_LogonSession WMI class. You can find an example here
(watch for line wrap):

http://groups.google.com/[email protected]#link2

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ChrisB4745 said:
I'm working on a system service application that is supposed to report
various information about the computer it is running on back to a central
server. One of the things I need to know is the username of the person who
is currently logged on to the actual computer. I've tried using
Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
However, since the service is owned by "System" I get
"NT_AUTHORITY\SYSTEM" as a result. So how do I get the username of the
person who is logged on instead of the name of the owner of the current
process?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top