我正在开发一个Windows服务,我想获得它执行的上下文。上下文我指的是执行它的用户帐户。 我尝试使用WindowsIdentity,但我没有得到我期望的价值。
'<a href="/' . urlencode($row['word']) . '.html" >' . $row['word'] . '</a>'
背景是我想记录执行服务的上下文,以便我可以记录正在发生的事情,并且可以在不允许服务的情况下以某种方式处理代码,这样我就不会这样做。我必须用try ... catch
填满所有内容所以问题是如何获得与服务相关联的用户帐户? 例如:administrator或localsystem account。
答案 0 :(得分:1)
您可以使用identity.Name
。对于localsystem帐户,其值为NT AUTHORITY\SYSTEM
。测试它的一些小代码可能看起来像这样:
FileStream fs = new FileStream("C:\\test.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
try
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
sw.Write(identity.Name);
}
catch (Exception ex)
{
sw.Write(ex.Message);
}
finally
{
sw.Flush(); sw.Dispose();
}