c# 如何擷取系統管理員許可權(UAC) 及判斷當前是否是管理員權限

來源:互聯網
上載者:User

標籤:

環境說明: 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

c# 如何擷取系統管理員許可權(UAC) 及判斷當前是否是管理員權限

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.