How C # Obtains system administrator rights (UAC) and determines whether it is currently administrator rights

Source: Internet
Author: User

Environment Description: Vs2012,windows 7 personally verified the Win7 and XP, XP directly does not frame, because XP does not have UAC control mechanism

Step 1:

Right-click Project-"Properties--" Security--select "Enable ClickOnce Security Settings"

At this point, under our project, there is a file called App.manifest under the properties directory.

Step 2:

The code in the file below, we just need to change the following sentence to

 <requestedexecutionlevel level= "Asinvoker< Span style= "color: #800000;" > "Uiaccess=" false Change to 
<requestedexecutionlevel level= "requireadministrator "Uiaccess=" false

AsInvoker: If you choose this, the application is running with the current permissions.

Highestavailable: This is run with the highest privileges available to the current user.

Requireadministrator: This is only run with system administrator privileges.

By default, it is AsInvoker.

Both the highestavailable and Requireadministrator options can prompt the user for system administrator privileges. So what's the difference between these two options?

The difference is that highestavailable is executed according to the permissions that the current account can get, while Requireadministrator is run with the full permissions of the administrator. If the current account is an administrator account, then both are allowed to gain access to administrator privileges through elevated privileges, and if the current account is guest, then highestavailable will abandon the elevated privileges and run directly, Requireadministrator, however, allows you to enter passwords for other administrator accounts to elevate permissions.

The following is the modified content:
<?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 manifest option if you want to change the Windows user Account Control level, replace the requestedExecutionLevel node with one of the following nodes. <requestedexecutionlevel level="AsInvoker"uiaccess="false"/> <requestedexecutionlevel level="requireadministrator"uiaccess="false"/> <requestedexecutionlevel level="highestavailable"uiaccess="false"/>specifying the requestedExecutionLevel node disables file and registry virtualization.        If you are using file and registry virtualization for backward compatibility, remove the requestedExecutionLevel node. -<requestedexecutionlevel level="requireadministrator"uiaccess="false"/> </requestedPrivileges> <applicationRequestMinimum> <defaultassemblyrequest Permissio Nsetreference="Custom"/> <permissionsetclass="System.Security.PermissionSet"version="1"Id="Custom"Samesite="None"/> </applicationRequestMinimum> </security> </trustInfo> <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1"> <application> <!--This application is designed to use a list of all versions of Windows. Windows will automatically select the most compatible environment. -<!--If the application is designed to use Windows Vista, uncomment the following Supportedos node-<!--<supportedos id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"></supportedOS>--> <!--If the application design uses Windows7, please uncomment the following Supportedos node-<!--<supportedos id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>--> <!--If the application is designed to use Windows8, please uncomment the following Supportedos node-<!--<supportedos id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"></supportedOS>--> </application> </compatibility> <!--the theme of enabling Windows Common Controls and dialog boxes (Windows XP and later)-<!--<dependency> <dependentAssembly> <assemblyidentity Type="Win32"name="Microsoft.windows.common-controls"version="6.0.0.0"processorarchitecture="*"PublicKeyToken="6595B64144CCF1DF"language="*"/> </dependentAssembly> </dependency>--></asmv1:assembly>


Step 3: Right-click on the project-"Properties-" security-"Remove the" Enable ClickOnce Security Settings "tick, or compile will error

After compiling, we change a non-administrator access account login system, open the program, you can see the need to provide administrator rights of the box.

Let's take a look at how the program knows if it is currently running on system administrator or non-system administrator rights:
Using System.Security.Principal

Verify that Win7 and XP can be done

public static bool Isadministrator ()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent ();
WindowsPrincipal principal = new WindowsPrincipal (identity);
return principal. IsInRole (Windowsbuiltinrole.administrator);
}

For further insights to attend:

. A summary of ways to elevate UAC permissions in Net-the great Lord Mayswind

How C # Obtains system administrator rights (UAC) and determines whether it is currently administrator rights

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.