c# - 如何从具有管理员权限的进程启动没有管理员权限的新进程?

标签 c# windows administrator

我正在为应用程序创建一个自动更新程序。该应用程序由用户启动,无需管理员权限即可运行。自动更新程序以管理员权限启动,并在下载新文件之前终止应用程序。

当我想在 autoupdater 完成后启动更新的应用程序时,问题就来了。如果我使用常规的 System.Diagnostics.Process.Start(file),应用程序也会以管理员权限启动,并且它必须在当前用户上运行才能按预期工作。

那么,如何让自动更新程序以当前用户而不是管理员身份启动应用程序呢?

我试过使用以下方法:

var pSI = new ProcessStartInfo() { 
    UseShellExecute = false, 
    UserName = Environment.UserName, 
    FileName = file 
};
System.Diagnostics.Process.Start(pSI);

但这会引发错误“无效的用户名或密码”。我已经检查过用户名是否正确,并且我知道密码可能无效,因为我没有包含它。但是要求用户输入他/她的密码不是一个选项,因为自动启动应用程序的全部原因是为了让用户更容易。

有什么建议吗?

最佳答案

您想要实现的目标无法轻易完成,并且不受支持。但是,可以使用少量黑客攻击。 Aaron Margosis 写了一篇 article描述一种技术。

要引用相关部分,您需要执行以下步骤:

  1. Enable the SeIncreaseQuotaPrivilege in your current token
  2. Get an HWND representing the desktop shell (GetShellWindow)
  3. Get the Process ID (PID) of the process associated with that window (GetWindowThreadProcessId)
  4. Open that process (OpenProcess)
  5. Get the access token from that process (OpenProcessToken)
  6. Make a primary token with that token (DuplicateTokenEx)
  7. Start the new process with that primary token (CreateProcessWithTokenW)

本文包含一些演示 C++ 源代码的下载链接,从中应该足够简单,可以将其转换为 C#。

关于c# - 如何从具有管理员权限的进程启动没有管理员权限的新进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169431/

相关文章:

c# - Windows Phone 8.1 的 "Isolated Storage"替代品(不是 silverlight)

c# - WPF C#设计题中绘制图表

windows - Delphi SelectDirectory与网络驱动器的奇怪行为

Rstudio Git bash 每次 pop

windows - 如何从当前文件夹以管理员权限运行 PowerShell?

c# - 检测何时跳出循环

c# - 如何上传到 imgur 用户的帐户

windows - 如何使用 Perl 在 Windows 中创建 unicode 文件名

mongodb - 如何创建将管理其数据库用户的数据库管理员

powershell - 自提升脚本+执行策略