c# - 是否可以在没有 Windows 服务的情况下使用服务帐户(域)在不同的用户(模拟)下运行代码?
问题描述
我们希望在服务帐户的上下文中运行一个进程。可以在没有 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);
}
}
}
有关更多信息和完整示例,我建议查看上面的链接,因为我不想引用整个示例。
更多的
推荐阅读
- javascript - 如何通过单击按钮在搜索的下方以相同的搜索宽度显示搜索列表
- python - 使用 bash 配置文件设置时,Flask 不检索 env 变量(但它适用于导出)
- javascript - HTML 更改选中复选框的背景颜色
- r - 条件值
- git - 如何从 repo https://github.com/cheat/cheat 在 Kali Linux 上安装和配置程序作弊?
- bash - Bash如何对名为filedaymonthyear.mp4的文件列表进行排序
- objective-c - GNUstep - 致命错误:在 Ubuntu 20.04 上找不到“objc/objc.h”文件
- javascript - 是否可以将图像放入路径并赋予其透视?
- python - Python合并两个可变大小的列表
- python - 如何使用 jinja2 和 python 中的 json 文件生成配置