Category: Windows Services

08/11/06

Permalink 08:18:49 pm, Categories: Microsoft.NET, Windows Services, 117 words   English (US)

How do I get the Username in Windows Services?

SYMPTOMS
If a Windows Service running under Local System Account, System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() returns NT AUTHOURITY\SYSTEM

You want to get who logged on to the machine?

CAUSE
Windows Service is running under Local System Account. Current user credentials will be NT AUTHOURITY\SYSTEM. You have to query WMI to get current users logged on to the machine.

WORKAROUND
To work around this problem, use following code.

ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT UserName, Name FROMWin32_ComputerSystem");

foreach (ManagementObject mo in searcher.Get()) {
if (mo["UserName"] != null) {
vUserName = mo["UserName"].ToString();
}
if (mo["Name"] != null) {
strComputer = mo["Name"].ToString();
}
}
searcher.Dispose();
searcher = null;

Email me if you want VB.NET or C++.NET Code

Blog - Technical Articles

This blog is to publish technical articles.

This allows you to keep track of all the technical posted on this system.

September 2010
Mon Tue Wed Thu Fri Sat Sun
<<  <   >  >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30      

Search

Archives

Misc

Syndicate this blog XML

What is RSS?

powered by
b2evolution