zoukankan      html  css  js  c++  java
  • c# 如何获取系统管理员权限(UAC) 及判断当前是否是管理员权限

    环境说明: VS2012,windows 7  亲自验证过win7 和xp ,XP直接不弹框,因为XP没有UAC控制机制

    步骤1:

    右键项目--》属性--》安全性--》选中【启用ClickOnce安全设置】

    此时在我们的项目下Properties目录下多了个叫 app.manifest  的文件

    步骤2:

    文件里面的代码如下,我们只需要将以下这句更改了即可

    <requestedExecutionLevel level="asInvoker" uiAccess="false" />
    改为
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

    asInvoker : 如果选这个,应用程序就是以当前的权限运行。

    
    

    highestAvailable: 这个是以当前用户可以获得的最高权限运行。

    
    

    requireAdministrator: 这个是仅以系统管理员权限运行。

    
    

    默认情况下是 asInvoker。

    highestAvailable 和 requireAdministrator 这两个选项都可以提示用户获取系统管理员权限。那么这两个选项的区别在哪里呢?

    
    

    区别即是,highestAvailable按当前账号能获取到的权限执行,而requireAdministrator则是以具有完整权限的管理员运 行。如果当前账户是管理员账户的话,那么两者都是可以的通过提升权限来获取到管理员权限的;而如果当前账户是Guest的话,那么 highestAvailable则放弃提升权限而直接运行,而requireAdministrator则允许输入其他管理员账户的密码来提升权限。

    
    
    以下是修改后的内容:
    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
          <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            <!-- UAC 清单选项
                如果要更改 Windows 用户帐户控制级别,请用以下节点之一替换 
                requestedExecutionLevel 节点。
    
            <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
            <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
    
                指定 requestedExecutionLevel 节点将会禁用文件和注册表虚拟化。
                如果要利用文件和注册表虚拟化实现向后 
                兼容性,则删除 requestedExecutionLevel 节点。
            -->
            <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
          </requestedPrivileges>
          <applicationRequestMinimum>
            <defaultAssemblyRequest permissionSetReference="Custom" />
            <PermissionSet class="System.Security.PermissionSet" version="1" ID="Custom" SameSite="none" />
          </applicationRequestMinimum>
        </security>
      </trustInfo>
      <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
          <!-- 此应用程序设计使用的所有 Windows 版本的列表。
          Windows 将会自动选择最兼容的环境。-->
          <!-- 如果应用程序设计为使用 Windows Vista,请取消注释以下 supportedOS 节点-->
          <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"></supportedOS>-->
          <!-- 如果应用程序设计使用 Windows 7,请取消注释以下 supportedOS 节点-->
          <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
          <!-- 如果应用程序设计为使用 Windows 8,请取消注释以下 supportedOS 节点-->
          <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>-->
        </application>
      </compatibility>
      <!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
      <!-- <dependency>
        <dependentAssembly>
          <assemblyIdentity
              type="win32"
              name="Microsoft.Windows.Common-Controls"
              version="6.0.0.0"
              processorArchitecture="*"
              publicKeyToken="6595b64144ccf1df"
              language="*"
            />
        </dependentAssembly>
      </dependency>-->
    </asmv1:assembly>


    步骤3:右键项目--》属性--》安全性--》去掉【启用ClickOnce安全设置】的勾,否则编译会报错

    编译后,我们换个非管理员权限的账号登陆系统,打开程序,可以看到需要提供管理员权限的弹框。

     

    下面再来看看程序如何知道当前运行在系统管理员权限还是非系统管理员权限:
    using System.Security.Principal

     

    //验证过win7 和xp  能行

    public static bool IsAdministrator()
    {
    WindowsIdentity identity = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }

    有关更进一步的见解参加:

    .NET中提升UAC权限的方法总结 - 大魔王mAysWINd

  • 相关阅读:
    linux默认的2.7升级到3.7版本
    linux 延时执行——at命令的几种用法
    unittest===unittest 的几种执行方式
    第一本docker书 学习笔记(二)
    第一本docker书 学习笔记(一)
    selenium===使用docker搭建selenium分布式测试环境
    https://www.yunpanjingling.com/
    AndroidManifest.xml权限设置
    XSS注入常用语句
    移动APP安全测试
  • 原文地址:https://www.cnblogs.com/fj99/p/4239690.html
Copyright © 2011-2022 走看看