【问题标题】:Run process as administrator from a non-admin application从非管理员应用程序以管理员身份运行进程
【发布时间】:2013-05-31 08:16:31
【问题描述】:

从不是以管理员身份运行的应用程序,我有以下代码:

ProcessStartInfo proc = new ProcessStartInfo();
proc.WindowStyle = ProcessWindowStyle.Normal;
proc.FileName = myExePath;
proc.CreateNoWindow = false;
proc.UseShellExecute = false;
proc.Verb = "runas";

当我调用 Process.Start(proc) 时,我没有弹出要求以管理员身份运行的权限,并且 exe 不是以管理员身份运行的。

我尝试将 app.manifest 添加到在 myExePath 中找到的可执行文件中,并将 requestedExecutionLevel 更新为

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

使用更新的 app.manifest,在 Process.Start(proc) 调用中,我得到一个异常,“请求的操作需要提升。”

为什么 .Verb 操作没有设置管理员权限?

我正在 Windows Server 2008 R2 Standard 上进行测试。

【问题讨论】:

  • 也许这会有所帮助? stackoverflow.com/questions/133379/…
  • Verb 仅适用于将UseShellExecute 设置为true
  • @DarkFalcon 好像已经完成了,谢谢。
  • @DarkFalcon 您需要将 UseShellExecute 设置为 true 才能尊重动词,并且必须将其设置为“false”以重定向标准输出。你不能两者都做。

标签: c# uac runas processstartinfo


【解决方案1】:

必须使用ShellExecute。 ShellExecute 是唯一知道如何启动 Consent.exe 以提升的 API。

示例 (.NET) 源代码

在 C# 中,调用 ShellExecute 的方式是使用 Process.StartUseShellExecute = true

private void button1_Click(object sender, EventArgs e)
{
   //Public domain; no attribution required.
   ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
   info.UseShellExecute = true;
   info.Verb = "runas";
   Process.Start(info);
}

如果你想成为一名优秀的开发者,你可以捕捉用户点击No的时间:

private void button1_Click(object sender, EventArgs e)
{
   //Public domain; no attribution required.
   const int ERROR_CANCELLED = 1223; //The operation was canceled by the user.

   ProcessStartInfo info = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
   info.UseShellExecute = true;
   info.Verb = "runas";
   try
   {
      Process.Start(info);
   }
   catch (Win32Exception ex)
   {
      if (ex.NativeErrorCode == ERROR_CANCELLED)
         MessageBox.Show("Why you no select Yes?");
      else
         throw;
   }
}

奖金观看

  • UAC - What. How. Why.。 UAC的架构,说明CreateProcess不能做提升,只能创建一个进程。 ShellExecute 是知道如何启动 Consent.exe 的人,而 Consent.exe 是检查组策略选项的人。

【讨论】:

  • 我尝试使用 UseShellExecute = false;动词 = "runas";重定向标准输入=真;域 = du[0];用户名 = 用户管理员;密码 = SecureStringHelper.ToSecureString(pwd);加载用户配置文件 = 真;并在清单中使用 requestedExecutionLevel。如果我使用 UseShellExecute = true;我收到错误 Process 对象必须将 UseShellExecute 属性设置为 false 才能以用户身份启动进程。
  • @Kiquenet 你必须设置UseShellExecute = true。 Windows ShellExecute 函数是唯一知道如何启动 Consent.exe 以提示管理员权限的函数。
  • 那么没有办法用其他用户凭据来提升进程?
  • @Jordan 你可以。您使用CreateProcessWithLogonW 启动一个非提升进程作为另一个进程。然后在申请时必须致电ShellExecute。 Microsoft 从未将其编写为单个函数的原因是,这意味着您正在做一些非常不安全的事情。您必须在某处硬编码用户密码。这很糟糕。阅读:Why Can’t I Elevate My Application to Run As Administrator While Using CreateProcessWithLogonW?
  • 所以我需要使用一个带有我要使用的凭据的中间进程,并使用这个进程来调用提升的进程?
猜你喜欢
  • 1970-01-01
  • 2011-09-24
  • 2010-12-19
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多