C# 以编程方式提升进程权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/133379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Elevating process privilege programmatically?
提问by Scott Marlowe
I'm trying to install a service using InstallUtil.exe but invoked through Process.Start
. Here's the code:
我正在尝试使用 InstallUtil.exe 安装服务,但通过Process.Start
. 这是代码:
ProcessStartInfo startInfo = new ProcessStartInfo (m_strInstallUtil, strExePath);
System.Diagnostics.Process.Start (startInfo);
where m_strInstallUtil
is the fully qualified path and exe to "InstallUtil.exe" and strExePath
is the fully qualified path/name to my service.
哪里m_strInstallUtil
是“InstallUtil.exe”的完全限定路径和exe,strExePath
是我的服务的完全限定路径/名称。
Running the command line syntax from an elevated command prompt works; running from my app (using the above code) does not. I assume I'm dealing with some process elevation issue, so how would I run my process in an elevated state? Do I need to look at ShellExecute
for this?
从提升的命令提示符运行命令行语法有效;从我的应用程序运行(使用上面的代码)没有。我假设我正在处理一些流程提升问题,那么我将如何在提升状态下运行我的流程?我需要看ShellExecute
这个吗?
This is all on Windows Vista. I am running the process in the VS2008 debugger elevated to admin privilege.
这一切都在 Windows Vista 上。我在提升为管理员权限的 VS2008 调试器中运行该进程。
I also tried setting startInfo.Verb = "runas";
but it didn't seem to solve the problem.
我也试过设置,startInfo.Verb = "runas";
但似乎没有解决问题。
采纳答案by mdb
You can indicate the new process should be started with elevated permissions by setting the Verb property of your startInfo object to 'runas', as follows:
您可以通过将 startInfo 对象的 Verb 属性设置为“runas”来指示新进程应该以提升的权限启动,如下所示:
startInfo.Verb = "runas";
This will cause Windows to behave as if the process has been started from Explorer with the "Run as Administrator" menu command.
这将导致 Windows 表现得好像该进程已使用“以管理员身份运行”菜单命令从资源管理器启动。
This does mean the UAC prompt will come up and will need to be acknowledged by the user: if this is undesirable (for example because it would happen in the middle of a lengthy process), you'll need to run your entire host process with elevated permissions by Create and Embed an Application Manifest (UAC)to require the 'highestAvailable' execution level: this will cause the UAC prompt to appear as soon as your app is started, and cause all child processes to run with elevated permissions without additional prompting.
这确实意味着会出现 UAC 提示并且需要用户确认:如果这是不受欢迎的(例如因为它会在一个漫长的过程中发生),你需要运行你的整个主机进程通过创建和嵌入应用程序清单 (UAC)来提升权限以要求“highestAvailable”执行级别:这将导致在您的应用程序启动后立即出现 UAC 提示,并导致所有子进程以提升的权限运行而无需额外提示.
Edit: I see you just edited your question to state that "runas" didn't work for you. That's really strange, as it should (and does for me in several production apps). Requiring the parent process to run with elevated rights by embedding the manifest should definitely work, though.
编辑:我看到您刚刚编辑了您的问题以说明“runas”对您不起作用。这真的很奇怪,因为它应该(并且在几个生产应用程序中对我来说也是如此)。不过,通过嵌入清单来要求父进程以提升的权限运行肯定是可行的。
回答by Vijesh VP
You should use Impersonation to elevate the state.
您应该使用模拟来提升状态。
WindowsIdentity identity = new WindowsIdentity(accessToken);
WindowsImpersonationContext context = identity.Impersonate();
Don't forget to undo the impersonated context when you are done.
完成后不要忘记撤消模拟的上下文。
回答by mdb
According to the article Chris Corio: Teach Your Apps To Play Nicely With Windows Vista User Account Control, MSDN Magazine, Jan. 2007, only ShellExecute
checks the embedded manifest and prompts the user for elevation if needed, while CreateProcess
and other APIs don't. Hope it helps.
根据文章Chris Corio:Teach Your Apps To Play Nice With Windows Vista User Account Control,MSDN 杂志,2007 年 1 月,仅ShellExecute
检查嵌入式清单并在需要时提示用户提升,而CreateProcess
其他 API 则不会。希望能帮助到你。
See also: same article as .chm.
另请参阅:与 .chm 相同的文章。
回答by hB0
[PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")]
This will do it without UAC - no need to start a new process. If the running user is member of Admin group as for my case.
这将在没有 UAC 的情况下完成 - 无需启动新流程。如果在我的情况下运行用户是 Admin 组的成员。
回答by Curtis Yallop
This code puts the above all together and restarts the current wpf app with admin privs:
此代码将上述所有内容放在一起,并使用管理员权限重新启动当前的 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)
Update: The app manifest way is preferred:
更新:首选应用清单方式:
Right click project in visual studio, add, new application manifest file, change the file so you have requireAdministrator set as shown in the above.
在visual studio中右键单击项目,添加新的应用程序清单文件,更改文件,以便您拥有如上所示的requireAdministrator设置。
A problem with the original way: If you put the restart code in app.xaml.cs OnStartup, it still may start the main window briefly even though Shutdown was called. My main window blew up if app.xaml.cs init was not run and in certain race conditions it would do this.
原来方式的一个问题:如果你把重启代码放在app.xaml.cs OnStartup中,即使调用了Shutdown,它仍然可能会短暂地启动主窗口。如果 app.xaml.cs init 没有运行,我的主窗口就会爆炸,并且在某些竞争条件下它会这样做。