如何仅在需要时提升权限? [英] How to elevate privileges only when required?

查看:16
本文介绍了如何仅在需要时提升权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题适用于 Windows Vista!

This question applies to Windows Vista!

我有一个应用程序,它通常在没有管理权限的情况下工作.有一项活动确实需要管理权限,但当我知道大部分时间用户甚至不会使用该功能时,我不想以更高的权限启动应用程序本身.

I have an application which normally works without administrative privileges. There is one activity which does need administrative privilege but I don't want to start the application itself with higher privileges when I know most of time user wont even be using that feature.

我正在考虑某种方法,通过该方法我可以在某些事件(例如按下按钮)上提升应用程序的权限.示例:

I am thinking about certain method by which I can elevate the privileges of application on some event (such as press of a button). Example:

如果用户单击此按钮,则会提示他提供 UAC 对话框或同意.我该怎么做?

If user clicks this button then he is prompted with UAC dialog or consent. How can I do this?

推荐答案

我不相信提升当前正在运行的进程是可能的.据我了解,Windows Vista 内置了在启动时向进程授予管理员权限的功能.如果您查看使用 UAC 的各种程序,您应该看到它们实际上在每次需要执行管理操作时启动一个单独的进程(任务管理器是一个,Paint.NET 是另一个,后者实际上是一个 .NET 应用程序).

I don't believe that it is possible to elevate the currently running process. It is built into Windows Vista that administrator privileges are given to a process upon startup, as I understand. If you look at various programs that utilise UAC, you should see that they actually launch a separate process each time an administrative action needs to be performed (Task Manager is one, Paint.NET is another, the latter being a .NET application in fact).

这个问题的典型解决方案是在启动提升的进程时指定命令行参数(abatishchev的建议是这样做的一种方法),这样启动的进程只知道显示某个对话框,然后在此之后退出动作已经完成.因此,用户几乎不会注意到一个新进程已经启动然后退出,而是看起来好像在同一个应用程序中打开了一个新对话框(特别是如果你有一些黑客来制作主窗口)提升进程是父进程的子进程).如果您不需要用于提升访问权限的 UI,那就更好了.

The typical solution to this problem is to specify command line arguments when launching an elevated process (abatishchev's suggestion is one way to do this), so that the launched process knows only to display a certain dialog box, and then quit after this action has been completed. Thus it should hardly be noticeable to the user that a new process has been launched and then exited, and would rather appear as if a new dialog box within the same app has been opened (especially if you some hackery to make the main window of the elevated process a child of the parent process). If you don't need UI for the elevated access, even better.

有关 Vista 上 UAC 的完整讨论,我建议您参阅 关于这个主题的文章(代码示例在 C++ 中,但我怀疑您需要使用WinAPI 和 P/Invoke 无论如何都可以在 C# 中完成大部分事情).希望您现在至少看到了正确的方法,尽管设计一个符合 UAC 的程序绝非易事...

For a full discussion of UAC on Vista, I recommend you see this very through article on the subject (code examples are in C++, but I suspect you'll need to use the WinAPI and P/Invoke to do most of the things in C# anyway). Hopefully you now at least see the right approach to take, though designing a UAC compliant program is far from trivial...

这篇关于如何仅在需要时提升权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆