delphi - 来自已提升应用程序的 UAC 提示

标签 delphi winapi uac

假设应用程序已经以提升的权限运行。 该应用程序是否可以显示UAC提示并获取其结果(成功确认或取消)?

背景故事:我有一个应用程序需要管理员权限,但在受限用户帐户中运行,因此在启动时会显示 UAC 提示,用户输入管理员凭据进行确认,一切正常。但是,对于某些关键操作,我想验证当前用户(仍然)是否允许执行此操作。

例如,原始用户在没有锁定其 Windows 帐户的情况下离开了工作站(是的,世界并不完美......),而另一个用户打开了已经运行的应用程序并访问了一些敏感设置。 您可以将其与在线商店进行比较,如果已登录的用户想要更改其送货地址,则必须再次提供其凭据。

我知道我可以创建自定义提示,要求提供管理员帐户凭据并检查它们是否有效,但我根本不想触及这些凭据。我也不想引入额外的特定于应用程序的凭据。 UAC 提示将是一个很好的 native 解决方案,用于重新验证用户是否具有管理员权限。

基本上是这样的:

if VerifyAdminWithUacPrompt then
begin
  //critical stuff
end;

Delphi 示例将是完美的,但我也很高兴了解如何实现此目标的一般想法。

最佳答案

您的应用程序不需要调用新的 UAC 提示,因为 UAC 已经在提升运行您的应用程序。该应用程序只需要询问用户凭据。 Windows 有专门用于此目的的 API:CredUIPromptForCredentials()CredUIPromptForWindowsCredentials() :

The CredUIPromptForCredentials function creates and displays a configurable dialog box that accepts credentials information from a user.

The CredUIPromptForWindowsCredentials function creates and displays a configurable dialog box that allows users to supply credential information by using any credential provider installed on the local computer.

参见Asking the User for Credentials在 MSDN 上了解更多详细信息:

Your application may need to prompt the user for user name and password information to avoid storing an administrator password or to verify that the token holds the appropriate privileges.

However, simply prompting for credentials may train users to supply those to any random, unidentified dialog box that appears on the screen. The following procedure is recommended to reduce that training effect.

To properly acquire user credentials

  • Inform the user, by using a message that is clearly part of your application, that they will see a dialog box that requests their user name and password. You can also use the CREDUI_INFO structure on the call to CredUIPromptForCredentials to convey identifying data or a message.

  • Call CredUIPromptForCredentials. Note that the maximum number of characters specified for user name and password information includes the terminating null character.

  • Call CredUIParseUserName and CredUIConfirmCredentials to verify that you obtained appropriate credentials.

关于delphi - 来自已提升应用程序的 UAC 提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55185301/

相关文章:

c++ - static ofstream 将创建一个文件但从不写入它?

windows - 以某种方式能够在 Windows 8 中启用 UAC 的情况下写入 ProgramData

delphi - X,Y 字符串的大量组合

Delphi:覆盖基本方法的代码完成

c - 两个不同显示器上的两个全屏窗口

c - GetFileSizeEx 在 System32 目录上返回不正确的值

windows - 通常从提升的进程执行进程

c++ - 如何运行需要提升并等待的子进程?

delphi - 如何调整 TrackBar 拇指大小?

delphi - 如何在 Delphi 中自动设置 bool 值?