【发布时间】:2013-07-24 15:40:32
【问题描述】:
我正在尝试使用 ServiceController 在另一台机器上管理服务。
var sc = new ServiceController(serviceName, machine);
Console.WriteLine(sc.Status);
由于我需要使用不同的凭据,我使用以下方式进行模拟:
var tokenHandle = IntPtr.Zero;
bool returnValue = LogonUser(userName, domainName, password,
LOGON32_LOGON_NEW_CREDENTIALS,
LOGON32_PROVIDER_DEFAULT,
ref tokenHandle);
if (!returnValue)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
WindowsIdentity newId = new WindowsIdentity(tokenHandle);
impersonatedUser = newId.Impersonate();
模拟似乎有效。但我不断收到 InvalidOperationException:
System.InvalidOperationException: Cannot open MyService service on computer 'TargetMachine'.
---> System.ComponentModel.Win32Exception: Access is denied
我的工作站在域中,而目标计算机参与了工作组。
知道我在这里缺少什么吗?
【问题讨论】:
-
访问被拒绝。当我没有管理员权限时,我就有了。在管理员模式下运行VS。如果它有效,那么您只需要授予您的程序管理员权限。
-
它非常适合本地服务。我也使用 [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
标签: c# service impersonation