Retreiving the logon account username for a particular windows service

T

tsmojver

Does anyone know how to retreive the logon account username for a
particular windows service? Or the current user that is running this
service as a process. C# is the prefered language of choice.

Apparently, the managed classes don't seem to offer this easily (and
for good reason). However, I would think there is a way to do this
with an unmanaged call, but not sure where to start.


Any help is greatly appreciated, Thanks.
 
A

Aneesh Pulukkul[MCSD.Net]

Does anyone know how to retreive the logon account username for a
particular windows service? Or the current user that is running this
service as a process. C# is the prefered language of choice.

Apparently, the managed classes don't seem to offer this easily (and
for good reason). However, I would think there is a way to do this
with an unmanaged call, but not sure where to start.

Any help is greatly appreciated, Thanks.

Inside the windows service code or you want it externally through some
code?
 
A

Aneesh Pulukkul[MCSD.Net]

Inside the windows service code or you want it externally through some
code?

Inside the windows service code:
Write to file/eventlog the username/domain using Enviroment.Username
and Environment.userDomain
Externaly:
Get list of running procecsses and check the account

Processes = Process.GetProcesses();//System.Diagnostics
foreach (Process p in Processes)
{
//Compare name - i dunno you would get user info from
process class. Will check.
}
 
T

tsmojver

Inside thewindowsservicecode:
Write to file/eventlog theusername/domain using Enviroment.Username
and Environment.userDomain
Externaly:
Get list of running procecsses and check theaccount

Processes = Process.GetProcesses();//System.Diagnostics
foreach (Process p in Processes)
{
//Compare name - i dunno you would get user info from
process class. Will check.
}

Thanks for the help. Would you know how to do this through external
code?
 
W

Willy Denoyette [MVP]

Does anyone know how to retreive the logon account username for a
particular windows service? Or the current user that is running this
service as a process. C# is the prefered language of choice.

Apparently, the managed classes don't seem to offer this easily (and
for good reason). However, I would think there is a way to do this
with an unmanaged call, but not sure where to start.


Any help is greatly appreciated, Thanks.


To get this service info from another program, you can use
System.Management, here is a complete sample that returns the logon account
of the Eventlog service.

using System;
using System.Management;
class Tester
{
static void Main()
{
SelectQuery query = new SelectQuery("select name, startname from
Win32_Service where name='Eventlog'");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
foreach (ManagementObject service in searcher.Get()) {
Console.WriteLine("Name: {0} - Logon : {1} ", service["Name"],
service["startname"]);
}
}
}
}


From within the Service process, you can simply use the
System.Security.Principal.WindowsIdentity class.
Following will return the identity of the running the process (if not
impersonating).
string currentId = WindowsIdentity.GetCurrent().Name;

Willy.
 
A

Aneesh Pulukkul[MCSD.Net]

Does anyone know how to retreive the logon account username for a
particular windows service? Or the current user that is running this
service as a process. C# is the prefered language of choice.
Apparently, the managed classes don't seem to offer this easily (and
for good reason). However, I would think there is a way to do this
with an unmanaged call, but not sure where to start.
Any help is greatly appreciated, Thanks.

To get this service info from another program, you can use
System.Management, here is a complete sample that returns the logon account
of the Eventlog service.

using System;
using System.Management;
class Tester
{
static void Main()
{
SelectQuery query = new SelectQuery("select name, startname from
Win32_Service where name='Eventlog'");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
foreach (ManagementObject service in searcher.Get()) {
Console.WriteLine("Name: {0} - Logon : {1} ", service["Name"],
service["startname"]);
}
}
}
}

From within the Service process, you can simply use the
System.Security.Principal.WindowsIdentity class.
Following will return the identity of the running the process (if not
impersonating).
string currentId = WindowsIdentity.GetCurrent().Name;

Willy.

That was great info Willy. I was searching in Process class. It does
not contain user account information. Wiil note this System.Management
namespace.
 
T

tsmojver

To get thisserviceinfo from another program, you can use
System.Management, here is a complete sample that returns thelogonaccount
of the Eventlogservice.
using System;
using System.Management;
class Tester
{
static void Main()
{
SelectQuery query = new SelectQuery("select name, startname from
Win32_Service where name='Eventlog'");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
foreach (ManagementObjectservicein searcher.Get()) {
Console.WriteLine("Name: {0} -Logon: {1} ",service["Name"],
service["startname"]);
}
}
}
}
From within theServiceprocess, you can simply use the
System.Security.Principal.WindowsIdentity class.
Following will return the identity of the running the process (if not
impersonating).
string currentId = WindowsIdentity.GetCurrent().Name;

That was great info Willy. I was searching in Process class. It does
not contain useraccountinformation. Wiil note this System.Management
namespace.- Hide quoted text -

- Show quoted text -

Excellent, that really saves me a lot of time. Thanks so much!
 

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