码迷,mamicode.com
首页 > 其他好文 > 详细

highestAvailable比较灵活,毕竟大多数功能不需要系统最高权限

时间:2015-08-17 23:15:13      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:

  打开VS2005、VS2008、VS2010工程,查看工程文件夹中的Properties文件夹下是否有app.manifest这个文件;如没有,按如下方式创建:鼠标右击工程在菜单中选择“属性”,点击工程属性的“安全性”标签,在安全性标签页中勾选“启用ClickOnce安全设置”,并选择“这是完全可信的应用程序”,保存工程,此时在Properties下已经自动生成了app.manifest文件。

        将默认的app.manifest文件修改为

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"   
  3.   
  4. xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"  
  5.   
  6.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
  7.   <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>  
  8.   <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">  
  9.     <security>  
  10.       <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">  
  11.         <!-- UAC Manifest Options  
  12.             If you want to change the Windows User Account Control level replace the   
  13.             requestedExecutionLevel node with one of the following.  
  14.         <requestedExecutionLevel  level="asInvoker" uiAccess="false" />  
  15.         <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />  
  16.         <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />  
  17.             If you want to utilize File and Registry Virtualization for backward   
  18.             compatibility then delete the requestedExecutionLevel node.  
  19.         -->  
  20.         <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  
  21.       </requestedPrivileges>  
  22.     </security>  
  23.   </trustInfo>  
  24. </asmv1:assembly>  

 

        配置文件修改后,我们运行应用程序,就会首先弹出这样一个提示框,点 Yes 后,程序才可以继续运行。

        顺便说下,还可以通过一个方法了解到此时程序运行是不是管理员权限:

 

[csharp] view plaincopy
 
  1. public static bool IsAdministrator()  
  2. {  
  3.     WindowsIdentity identity = WindowsIdentity.GetCurrent();  
  4.     WindowsPrincipal principal = new WindowsPrincipal(identity);  
  5.     return principal.IsInRole(WindowsBuiltInRole.Administrator);  
  6. }  


对于XML文件中引用的UAC执行权限级别,分别代表下列含义:

 

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.

 

         asInvoker : 应用程序就是以当前的权限运行。

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

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

         默认情况下是 asInvoker。

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

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

 

参考:http://blog.csdn.net/a316019667/article/details/8647237

----------------------------------------------------------------------------

uiAccess Values

Possible uiAccess values

Value

Description

False

The application does not need to drive input to the user interface of another window on the desktop. Applications that are not providing accessibility should set this flag to false. Applications that are required to drive input to other windows on the desktop (on-screen keyboard, for example) should set this value to true.

True

The application is allowed to bypass user interface control levels to drive input to higher privilege windows on the desktop. This setting should only be used for user interface Assistive Technology applications.

技术分享Important Note:

Applications with the uiAccess flag set to true must be Authenticode signed to start properly. In addition, the application must reside in a protected location in the file system. \Program Files\ and \windows\system32\ are currently the two allowable protected locations.

参考:http://www.cnblogs.com/wangjei155/archive/2009/09/29/1576551.html

----------------------------------------------------------------------------

Delphi:Delphi程序必须在资源里面嵌入MANIFEST信息。

?         首先编辑一个文件,内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> 

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">

    <security>

      <requestedPrivileges>

        <requestedExecutionLevel level="requireAdministrator"/>

      </requestedPrivileges>

    </security>

</trustInfo>

</assembly>

保存为UAC.manifest,这里文件是随意的。特别注意红色的“requireAdministrator”,这个表示程序需要管理员(Administrator)才能正常运行。

?         然后编辑一个RC文件,名称为uac.rc 如下所示:

1 24 UAC.manifest

其中:

1-代表资源编号

24-资源类型为RTMAINIFEST

UAC.manifest-前面的文件名称

?         用brcc32编译这个rc文件为res文件,如下所示:

brcc32 uac.rc -fouac.res

?         在程序里面加入

{$R uac.res}

让Delphi编译的时候,把uac.res编译进exe文件

?         把文件放到vista或win7下运行,就会看程序图标下面显示UAC盾牌标志了

 

注:直接修改exe属性,试了好像不灵啊。

参考:http://www.sieye.cn/showArticle.asp?nameID=201192722213483

----------------------------------------------------------------------------

UAC的存在更多的意义在于让程序员明白一个程序不要申请多余的权限,这样可以在最大程度上保护用户的安全。正常的做法就是把用户数据配置文件放在AppData下,只是很多人都是以XP的惯性思维来做。

至于2005下加入manifest的方法,作者最后给的那个链接文章里面也提到了,通过命令行的方式调用mt.exe即可:
mt.exe –manifest temp.manifest –outputresource:YourApp.exe;#1.

参考:http://www.cnblogs.com/boyliupan/archive/2011/01/17/1937518.html

highestAvailable比较灵活,毕竟大多数功能不需要系统最高权限

标签:

原文地址:http://www.cnblogs.com/findumars/p/4738017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!