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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM