zoukankan      html  css  js  c++  java
  • 以管理员身份运行应用程序

      在 Vista 和 Windows 7 及更新版本的操作系统,增加了 UAC (用户账户控制) 的安全机制,如果 UAC 被打开,用户即使以管理员权限登录,其应用程序默认情况下也无法对系统目录、系统注册表等可能影响系统正常运行的设置进行写操作。这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭 UAC ,但有时我们开发的应用程序又需要以 Administrator 的方式运行,这就需要启用 ClickOnce 安全设置。

      首先打开解决方案资源管理器中的 Properties 。在左侧标签栏中选择”安全性“选项,并选择”启用 ClickOnce 安全设置“。

     

      接着更改 app.manifest 。打开 Properties 下的 app.manifest ,可以看到许多关于程序启动的配置。其中有一段是这样的:

    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <!-- UAC 清单选项
                 如果想要更改 Windows 用户帐户控制级别,请使用
                 以下节点之一替换 requestedExecutionLevel 节点。n
            <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
            <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
    
                指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
                如果你的应用程序需要此虚拟化来实现向后兼容性,则删除此
                元素。
            -->
            <requestedExecutionLevel level="asInvoker" uiAccess="false" />
    </requestedPrivileges>

      我们可以看到这个配置中有一个 requestedExecutionLevel 项,这个项用于配置当前应用请求的执行权限级别。这个项有3个值可供选择,如下表所示:

    Value Description Comment
    asInvoker The application runs with the same access token as the parent process. Recommended for standard user applications. Do refractoring with internal elevation points, as per the guidance provided earlier in this document.
    highestAvailable The application runs with the highest privileges the current user can obtain. Recommended for mixed-mode applications. Plan to refractor the application in a future release.
    requireAdministrator The application runs only for administrators and requires that the application be launched with the full access token of an administrator. Recommended for administrator only applications. Internal elevation points are not needed. The application is already running elevated.

    1. asInvoker :以当前的权限运行。

    2. highestAvailable: 以当前用户可以获得的最高权限运行。

    3. requireAdministrator: 仅以系统管理员权限运行。

      默认情况下是 asInvoker。

      highestAvailable 和 requireAdministrator 这两个选项的区别在于,如果我们不是以管理员帐号登录,那么如果应用程序设置为 requireAdministrator ,那么应用程序就直接运行失败,无法启动。而如果设置为 highestAvailable ,则应用程序可以运行成功,但是是以当前帐号的权限运行而不是系统管理员权限运行。如果我们希望程序在非管理员帐号登录时也可以运行(这种情况下应该某些功能受限制) ,那么建议采用 highestAvailable 来配置。

      所以,只要把设定启动权限的一行:

    <requestedExecutionLevel level="asInvoker" uiAccess="false" />

      改为  

    <requestedExecutionLevel level="asInvoker" uiAccess="false" />

      就可以以管理员权限启动了。 

       还有一种方法判断是否已管理员身份启动,如果返回为 true 则为管理员身份启动:

    public static bool IsAdministrator()
    {
        return new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
    }
  • 相关阅读:
    先不说 console,其实你连 console.log 都不会
    2019 年终总结 & 2020 年度计划
    将毫秒格式化为天、小时、分钟、秒
    山村老事
    快速更改对象中的字段名
    基于 ECharts 封装甘特图并实现自动滚屏
    JS 将数值取整为10的倍数
    Flutter 徐徐图之(一)—— 从搭建开发环境到 Hello World
    Vue-Cli 3.x 创建的项目中对 import 引入的 CSS 样式启用 autoprefixer
    word——插入目录
  • 原文地址:https://www.cnblogs.com/Bita/p/5572319.html
Copyright © 2011-2022 走看看