如何以“标准用户”身份复制文件 在 Vista(即“管理选择应用程序”)中通过提示用户输入管理员凭据?

发布于 2024-07-25 04:36:26 字数 1258 浏览 9 评论 0原文

我有 UAC 开发指南中所说的“管理选择应用程序”。 如果您熟悉这是什么,请跳到下一部分。

背景:

我想让“标准”用户能够在我的应用程序的首选项中选择/取消选择“启动时运行”选项。

由于我的应用程序是针对每台计算机(而不是针对每个用户)的,因此需要将快捷方式文件删除或复制到“开始”菜单/程序/启动文件夹中,这将需要管理访问权限才能执行此操作。

因此,我想要的是出现“用户帐户控制凭据提示”,这样如果用户拥有管理员帐户,他们也可以输入凭据。 显然,这就是应用程序应该设计的方式,以防止用户每次需要执行某些管理操作时都必须切换到另一个帐户。

摘自 MSDN 文档:

管理选择应用程序

提升的进程或 COM 对象

初始应用程序无需提升即可启动。 用户界面中需要管理访问令牌的那些项目用盾牌图标装饰作为标识。 此装饰向用户表明使用该功能需要管理员批准。 当应用程序检测到已选择这些按钮之一时,它有以下两种选择。

  • 应用程序使用 ShellExecute() 启动第二个程序来执行管理任务。 第二个程序将标记为 requireAdministrator 的 requestExecutionLevel,从而提示用户进行批准。 第二个程序将使用完整的管理访问令牌运行,并且能够执行所需的任务。 - 或 -
  • 应用程序使用 CreateElevatedComObject() 启动 COM 对象。 该 API 将在批准后使用完整的管理访问令牌启动 COM 对象,并且该 COM 对象将能够执行所需的任务。

我只需要复制一个文件...使用 ShellExecute() 分叉一个新进程似乎有点过分,而且我对 COM 的了解不够,不知道是否可以使用它来复制文件。 我希望有人可以发布一些代码,提供复制文件的方法,并且最好还解释如何使用“盾牌装饰器”装饰 MenuItem。

注意:

我查看了微软提供的UAC Demo,它是在多个 StackOverflow 帖子中引用,例如 (如果路径为,则请求 Windows Vista UAC 提升protected?) 与权限相关的主题。 该代码仅包含调用单独进程的示例。

I have what the UAC Development Guides refer to as a "Administrative Choice Application." If you're familiar with what this is skip to the next section.

Background:

I want to let a "Standard" user have the ability to select/deselect a Run On Startup option in the preferences for my application.

Since my application is per machine (not per user), what needs to happen is it will either need to Delete or Copy a shortcut file into the Start Menu/Programs/Startup folder which will require administrative access to perform this operation.

So, what i'd like is for the "User Account Control credential prompt" to appear and that way if the user has an admin account also they can put in the credentials. This is apparently how applications are supposed to be designed to keep the user from having to switch to another account each time they need to do something administrative.

Excerpt from MSDN documentation:

An Administrative Choice Application

An Elevated Process or COM Object

The initial application launches without requiring elevation. Those items in the user interface that would require an administrative access token are decorated with a shield icon as identification. This decoration indicates to the user that using that feature will require administrator approval. When the application detects that one of these buttons has been selected, it has the following two choices.

  • The application launches a second program using ShellExecute() to perform the administrative task. This second program would be marked with a requestedExecutionLevel of requireAdministrator, thus causing the user to be prompted for approval. This second program would be running with a full administrative access token and would be able to perform the desired task.
    -OR-
  • The application launches a COM object using CreateElevatedComObject(). This API would launch the COM object with a full administrative access token following approval and this COM object would be able to perform the desired task.

I just need to copy a file... seems excessive to fork a new process using ShellExecute() and I don't know enough about COM to know if I could use it to copy a file. I am hoping someone can post some code which provides a way to copy the file and ideally also explain how to decorate a MenuItem with the "sheild decorator".

Notes:

I have looked at the UAC Demo provided by microsoft which is referenced in several StackOverflow posts such as (Request Windows Vista UAC elevation if path is protected?) on topics related to permissions. The code only has an example of the calling a separate process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-08-01 04:36:27

尽管它似乎仍然至少涉及重新启动或生成进程,但您可以在这里找到一些帮助:
CodeProject 上的 UAC Shield for Elevation .com

Though it still appears to involve at least restarting or spawning a process, you can find some help here:
UAC Shield for Elevation at CodeProject.com

死开点丶别碍眼 2024-08-01 04:36:27

我最终走向了不同的方向。 我让安装程序在“所有用户/启动”文件夹中创建一个启动快捷方式,将参数传递给应用程序“启动”。

当应用程序启动时,我将检查 arg[0].equals("startup") 是否存在,然后检查 Settings1.Default.RunOnStartup == true 是否存在。

如果这两个条件都成立,我会立即退出应用程序。 当应用程序在没有该参数(即开始菜单程序组)的情况下启动时,应用程序会正常加载。

RunOnStartup 设置是用户范围的设置,因此每个用户都可以更改而不影响其他用户。

I ended up going in a different direction. I had my installer create a startup shortcut in the All Users/Startup folder that passes an argument to the application "startup".

When the application started I would check for the existence of the arg[0].equals("startup") and then check if Settings1.Default.RunOnStartup == true.

If both conditions were true I'd exit the application immediately. When the application is started without that argument (ie the Start Menu Program Group), the application loaded normally.

The RunOnStartup setting is a user scoped setting so each user can change without effecting others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文