代码之家  ›  专栏  ›  技术社区  ›  Scott Marlowe

以程序方式提升过程特权?

  •  128
  • Scott Marlowe  · 技术社区  · 16 年前

    我正在尝试使用installUtil.exe安装服务,但通过调用 Process.Start . 代码如下:

    ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath);
    System.Diagnostics.Process.Start (startInfo);
    

    哪里 m_strInstallUtil 是“installUtil.exe”的完全限定路径和exe,并且 strExePath 是我的服务的完全限定路径/名称。

    从提升的命令提示符运行命令行语法是有效的;从我的应用程序运行(使用上面的代码)是无效的。我假设我正在处理一些进程提升问题,那么如何在提升状态下运行我的进程?我需要看看吗 ShellExecute 为了这个?

    这些都在Windows Vista上。我正在提升为管理员权限的VS2008调试器中运行该进程。

    我也试过设置 startInfo.Verb = "runas"; 但似乎解决不了这个问题。

    5 回复  |  直到 6 年前
        1
  •  158
  •   Mojtaba Rezaeian    9 年前

    通过将StartInfo对象的Verb属性设置为“runas”,可以指示新进程应以提升的权限启动,如下所示:

    startInfo.Verb = "runas";
    

    这将导致Windows的运行方式与使用“以管理员身份运行”菜单命令从资源管理器启动进程的方式相同。

    这意味着UAC提示将出现并需要用户确认:如果这是不需要的(例如,因为它将发生在一个冗长的过程中),您将需要在 Create and Embed an Application Manifest (UAC) 要求“highestavailable”执行级别:这将导致应用程序启动后立即出现UAC提示,并导致所有子进程以提升的权限运行,而无需其他提示。

    编辑:我看到你刚刚编辑了你的问题,说“runas”不适合你。这真的很奇怪,因为它应该(在一些生产应用程序中对我也是如此)。但是,通过嵌入清单,要求父进程以提升的权限运行肯定是可行的。

        2
  •  39
  •   Curtis Yallop    12 年前

    此代码将上述所有内容放在一起,并使用管理员权限重新启动当前的WPF应用程序:

    if (IsAdministrator() == false)
    {
        // Restart program and run as admin
        var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        ProcessStartInfo startInfo = new ProcessStartInfo(exeName);
        startInfo.Verb = "runas";
        System.Diagnostics.Process.Start(startInfo);
        Application.Current.Shutdown();
        return;
    }
    
    private static bool IsAdministrator()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    
    
    // To run as admin, alter exe manifest file after building.
    // Or create shortcut with "as admin" checked.
    // Or ShellExecute(C# Process.Start) can elevate - use verb "runas".
    // Or an elevate vbs script can launch programs as admin.
    // (does not work: "runas /user:admin" from cmd-line prompts for admin pass)
    

    更新:首选应用程序清单方式:

    右键单击Visual Studio中的“项目”、“添加”、“新建应用程序清单文件”,更改该文件,使您具有如上所示的RequireAdministrator设置。

    原始方法的一个问题:如果将重新启动代码放入app.xaml.cs onstartup中,即使调用了shutdown,它仍可能短暂地启动主窗口。如果app.xaml.cs in it没有运行,我的主窗口就会崩溃,在某些比赛条件下,它会这样做。

        3
  •  20
  •   Kuba hasn't forgotten Monica    6 年前

    根据文章 Chris Corio: Teach Your Apps To Play Nicely With Windows Vista User Account Control, MSDN Magazine, Jan. 2007 ,只有 ShellExecute 检查嵌入的清单并在需要时提示用户提升,同时 CreateProcess 其他的原料药没有。希望能有所帮助。

    参见: same article as .chm .

        4
  •  6
  •   Gonzalo.-    12 年前
    [PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
    

    这将在无UAC的情况下完成——无需启动新流程。如果正在运行的用户是管理组的memeber。

        5
  •  1
  •   Leon Bambrick jon Z    14 年前

    您应该使用模拟来提升状态。

    WindowsIdentity identity = new WindowsIdentity(accessToken);
    WindowsImpersonationContext context = identity.Impersonate();
    

    完成后不要忘记撤消模拟的上下文。