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
For example:
You want to get a client name from the client list. "GetClientName" function exposed through web service name called "Service1.asmx" and the function takes "Client Code" as a passing variable.
Copy the WebService behavior (webservice.htc file) into the Web project folder.
You can download webservice.htc file at this link: http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/samples/internet/behaviors/library/webservice/default.asp
Copy this javscript code and name it webservice.js and copy into the Web project folder.
var iCallID1;
function onBlur()
{
Get_ClientName();
}function onEnter()
{
var kCode=event.keyCode;
if(kCode==13)
{
Get_ClientName();
}
}function onWSresult()
{
if (event.result.error)
{
var xfaultcode = event.result.errorDetail.code;
var xfaultstring = event.result.errorDetail.string;
var xfaultsoap = event.result.errorDetail.raw;alert("Error: " + xfaultcode + "|" + xfaultstring + "|" + xfaultsoap + "|");
}
else
{if (iCallID1==event.result.id) {
document.all("ClientName").innerHTML = event.result.value;
}
}
}function Get_ClientName() {
if (document.all("ClientCode").value != "") {
service.useService("http://www.charora.com/WebServices/Service1.asmx?WSDL","MyWebService");
iCallID1 = service.MyWebService.callService("GetClientName", document.all("ClientCode").value);
}
else {
alert ("Please enter client code ... ");
}
}
Create test.htm and include webservice.js in the test.htm
add a DIV tag and with following attributes
id="service"
style="behavior:url(webservice.htc)"
onresult="onWSresult();"
add input tag and with following attributes
type="text"
name="ClientCode"
Text=""
onblur="onBlur();"
onkeypress="onEnter();"
add another input tag and with following attributes
type="text"
name="ClientName"
Text=""
Finally, browse the test.htm file and enter client code and hit enter.
You will get the client name.
This blog is to publish technical articles.
This allows you to keep track of all the technical posted on this system.
| 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 | 31 | |