[英]Is running code under a different user (impersonation) possible with a service account (domain) without a windows service?
我們想在服務帳戶的上下文中運行一個進程。 可以在沒有 windows 服務的情況下使用服務用戶帳戶(域)模擬 WPF 應用程序中的方法(使用 Process.Start)嗎?
如果可能,如何實現?
操作:
可以在沒有 windows 服務的情況下使用服務用戶帳戶(域)模擬 WPF 應用程序中的方法(使用 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);
}
}
}
有關更多信息和完整示例,我建議查看上面的鏈接,因為我不想引用整個示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.