UAC、需要管理员和文件访问权限

发布于 2024-10-31 00:54:05 字数 857 浏览 8 评论 0 原文

我在 Vista/7 上遇到 UAC 问题。

我的 UAC 执行级别 requireAdministrator 安装程序安装并运行我的应用程序。

  • 应用程序 UAC 执行级别:asInvoker

因此,安装程序第一次运行应用程序时,它会以管理员身份运行。然后,应用程序创建一些文件来存储自身状态。

稍后(如果应用程序由用户启动,而不是以管理员身份启动)应用程序只能读取创建的文件,但不能覆盖它们。

我尝试在应用程序清单中禁用 UAC,或者在没有清单的情况下构建应用程序,但结果仍然相同 - 当应用程序以用户身份运行时,以管理员身份运行应用程序时创建的每个文件都不能被覆盖。我尝试了一些其他安装程序,例如 Inno Setup,但没有什么像我的问题一样......所以我的问题 - 为什么它发生在我的情况以及如何修复它?谢谢。

PS 重要提示

  1. 实际上它不是安装程序。这是更新主应用程序可执行文件的实用程序。主应用程序检查服务器是否有更新(如果有)——将更新下载到临时文件夹,然后以提升的权限启动实用程序 (http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx) 以替换程序文件中的可执行文件 文件夹。主应用程序在启动实用程序后立即终止。
  2. 应用程序的所有文件都存储在 ProgramData\myAppName 文件夹中。

I have a problem with UAC on Vista/7.

My installer with UAC Execution Level requireAdministrator installs and runs my app.

  • App UAC Execution Level: asInvoker

So first time app runned by installer, it runs as administrator. Then, app creates some files to store self state.

Later (if app launched by user, not as administrator) app can only read from created files, but can't overwrite them.

I tried to disable UAC in the app manifest, or build app without manifest but result still same -- every file created when app runned as administrator can't be overwritten when app runs as user. I tried some other installers like Inno Setup but there nothing like my problem... So my question -- why it happens in my case and how to fix it ? Thanks.

P.S. Important notes

  1. Actually it is not installer. This is utility to update main app executables. Main app check server for updates, if any available -- downloads updates to the temp folder and then launch utility with elevated rights (http://www.codeproject.com/KB/vista-security/UAC__The_Definitive_Guide.aspx) in order to replace executables in the Program Files folder. Main app terminates just after launching utility.
  2. All files application stores in the ProgramData\myAppName folder.

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

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

发布评论

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

评论(2

星軌x 2024-11-07 00:54:05

您安装的应用程序是仅针对当前用户还是针对计算机上的所有用户?

对于每用户安装,安装程序在用户的 AppData 文件夹中创建初始应用程序状态是有意义的。 MSI 以非提升方式运行每用户安装,因此应用程序无法修改的 AppData 文件夹中的文件没有问题。

对于每台计算机安装,将任何内容放入特定用户的 AppData 文件夹或用户配置文件位置下的任何位置是没有意义的。新用户可以在安装应用程序后登录计算机,并且他们不会有任何东西。

也就是说,您有三种解决方案(实际上是两种解决方案和一种 hack):

  1. 使用每用户安装。设置 ALLUSERS=2 和 MSIINSTALLPERUSER=1。您的 MSI 将在非提升状态下运行。 MSI 可以随意将任何状态文件放入 AppData 文件夹中。
  2. 使用每台机器安装。不要写入用户的应用程序数据。相反,将初始状态文件写入 %ALLUSERSPROFILE%。它们对于应用程序来说是只读的。修改您的应用程序,以便在首次运行时将初始状态从 %ALLUSERPROFILE% 复制到当前用户配置文件。
  3. 将自定义操作添加到安装程序以将用户添加到 ACL。

(#3) 是一种黑客行为,我不推荐它,因为您的应用程序对于新用户或用户在计算机上重新创建其个人资料时会被破坏。

Are your installing application for current user only or for all users on the machine?

For per-user installation it makes sense for the installer to create initial application state in the user's AppData folder. MSI runs per-user installations NON-elevated, so there is no problem with files in AppData folder your applications cannot modify.

For per-machine installation it does not make sense to put anything to the specific user's AppData folder or anywhere under users profile location. New users can logon on the machine after application is installed and they would not have anything there.

That said, you have three solutions (actually two solutions and one hack):

  1. Use per-user installation. Set ALLUSERS=2 and MSIINSTALLPERUSER=1. Your MSI will be run non-elevated. MSI is free to put any state files in AppData folder.
  2. Use per-machine installation. Don't write to users's appdata. Instead, write initial state files into %ALLUSERSPROFILE%. They will be Read-only to application. Modify your application to copy initial state from %ALLUSERPROFILE% to current user profile on first run.
  3. Add custom action to your installer to add user to ACLs.

(#3) is a hack and I don't recommend it, because your application would be broken for new users, or when user re-creates his profile on machine.

望她远 2024-11-07 00:54:05

如果不需要管理员权限,则不应以提升的方式启动主应用程序。您使用什么类型的安装程序?

MSI 以当前用户权限运行,并且仅在对系统进行更改时进行提升。安装完成后,您可以启动主应用程序,它将以当前用户身份运行,而不是提升权限。

如果您自定义 setup.exe,则可以以非提升方式启动安装程序。当需要安装文件时,启动另一个进程(可以是相同的可执行文件)来安装文件并进行系统范围的更改。完成此步骤后,您可以从初始进程中以非提升状态重新启动主应用程序。

或者,使用当前安装程序和更新程序运行提升的方法,请按照 如何从桌面用户身份启动程序提升的应用启动您的主应用非提升

注意:ProgramData 的默认权限允许每个人创建文件和文件夹,但只有创建它们的用户可以修改文件(或管理员)。为了克服这个问题,您有两个选择:

  1. 直接在用户配置文件中创建数据文件。每个用户都会有自己的数据。
  2. 按照 Seva 在选项中建议的方式,从 ProgramData 中存储的通用模板创建用户配置文件数据。 2.。在这种情况下,每个用户也将拥有自己的数据。
  3. 或者修改 ProgramData 目录中应用程序数据的 ACL,以允许每个人(而不仅仅是创建文件夹和文件的用户)对其进行修改。

You should not start your main app elevated if it doesn't require administrator rights. What type of installer do you use?

MSI runs with current user privileges and elevates only to make changes to the system. After installation completes, you can start your main app and it will be run as the current user, non-elevated.

If you custom setup.exe, you can start your setup program non-elevated. When the time comes to install files, start another process (it can be the same executable) to install files and make system-wide changes. When this step completes, you can re-start your main app as non-elevated from the initial process.

Alternatively, with your current approach where setup and updater run elevated, follow the instructions at How do I start a program as the desktop user from an elevated app to start your main app non-elevated.

Note: the default permissions on ProgramData allow everyone to create files and folders but only the user that created them can modify the files (or administrator). To overcome this, you have two options:

  1. Create data files directly in user profile. Each user will have their own data.
  2. Create user profile data from common template stored in ProgramData, as suggested by Seva in option 2. In this case each user will also have their own data.
  3. Or modify ACLs on your application data in ProgramData directory to allow its modification by everyone, not just the user that created the folder and files.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文