在没有windows服务的情况下,是否可以使用服务帐户(域)在其他用户(模拟)下运行代码



我们希望在服务帐户的上下文下运行流程。WPF应用程序中的方法是否可以在没有windows服务的情况下使用服务用户帐户(域(模拟执行(使用Process.Start(?

如果可能的话,如何做到这一点?

OP:

WPF应用程序中的方法是否可以在没有windows服务的情况下使用服务用户帐户(域(模拟执行(使用Process.Start(?

无论调用进程的类型如何,您都可以模拟用户。例如WPF、Windows服务、控制台应用程序。这并不重要。但是,在Windows Vista及更高版本上,进程必须以管理员身份运行。

MSDN 提供的示例

string userName, domainName;
// Get the user token for the specified user, domain, and password using the
// unmanaged LogonUser method.
// The local machine name can be used for the domain name to impersonate a user on this machine.
Console.Write("Enter the name of the domain on which to log on: ");
domainName = Console.ReadLine();
Console.Write("Enter the login of a user on {0} that you wish to impersonate: ", domainName);
userName = Console.ReadLine();
Console.Write("Enter the password for {0}: ", userName);
...
// Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(userName, domainName, Console.ReadLine(),
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);
...
using (safeTokenHandle)
{
...
using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
{
using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
{
// Check the identity.
Console.WriteLine("After impersonation: "
+ WindowsIdentity.GetCurrent().Name);
}
}
}

有关更多信息和完整示例,我建议查看上面的链接,因为我不想引用整个示例。

更多

  • WindowsImpersonationContext类
  • 模拟和还原

相关内容

最新更新