473,395 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Retreiving the logon account username for a particular windows service

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.

Jun 8 '07 #1
6 6363
On Jun 8, 8:33 pm, tsmoj...@gmail.com wrote:
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?

Jun 8 '07 #2
On Jun 8, 8:44 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On Jun 8, 8:33 pm, tsmoj...@gmail.com wrote:
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?
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.
}

Jun 8 '07 #3
On Jun 8, 11:53 am, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On Jun 8, 8:44 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On Jun 8, 8:33 pm, tsmoj...@gmail.com wrote:
Does anyone know how to retreive thelogonaccountusernamefor a
>particularwindowsservice? Or the current user that is running this
>serviceas 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 thewindowsservicecode or you want it externally through some
code?

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?

Jun 8 '07 #4
<ts******@gmail.comwrote in message
news:11**********************@n4g2000hsb.googlegro ups.com...
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.

Jun 8 '07 #5
On Jun 8, 11:01 pm, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
<tsmoj...@gmail.comwrote in message

news:11**********************@n4g2000hsb.googlegro ups.com...
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.

Jun 8 '07 #6
On Jun 8, 2:37 pm, "Aneesh Pulukkul[MCSD.Net]" <anees...@gmail.com>
wrote:
On Jun 8, 11:01 pm, "Willy Denoyette [MVP]"

<willy.denoye...@telenet.bewrote:
<tsmoj...@gmail.comwrote in message
news:11**********************@n4g2000hsb.googlegro ups.com...
Does anyone know how to retreive thelogonaccountusernamefor a
>particularwindowsservice? Or the current user that is running this
>serviceas 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 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;
Willy.

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!

Jun 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Fabrizio | last post by:
(Sorry for the crosspost, but I really don't know which is the right newsgroup!) Hi all, I try to change the password to a user that as to change the password at first logon: try {
3
by: Sateesh | last post by:
Hello, I have a .Net service registered in my machine and set Logon type as "This account" and gave my domain account name and it's password. It works fine and I am able to connect to a remote...
10
by: et | last post by:
I have an asp.net program that uses a connection string, using integrated security to connect to a sql database. It runs fine on one server, but the other server gives me the error that "Login...
2
by: J-T | last post by:
Hi All, We have an asp.net application on a windows 2003 server which is part of a domain controller which my worksatis is too. We have impersonated in our applciation with a fixed identity...
9
by: Tim Baley | last post by:
I recently created some intranet web sevices to expose stored procedures on our SQL Servers (SQL2k). The stored procedures include an audit trail with user/login identification, so the web...
2
by: Radu | last post by:
Hi. I have created a service which I needed to install. Therefore I use InstallUtil. On my dev machine at home I login as Administrator and I have *NO* password set. In my first attempts with...
0
by: robpimentel | last post by:
Hi, I've been using DB2 for about 1 week, so please bear with me. DB2 Connect Enterprise Edition v8.1 FixPack 5 Windows Server 2003 Standard Edition SP1 Here is an error that continues to...
18
by: Arthur | last post by:
Hi All, I would like to get the name of the user given their networkID, is this something Active Directory would be useful for?(For intranet users) If so, can you please point me to some sample...
3
by: Lee T. Hawkins | last post by:
I am having a number of problems over the last two full days trying to get an ASP.NET 2.0 application to connect to a SQL Server 2005 database... First off, I built this application w/ Visual...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.