Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run .NET application as administrator

Tags:

.net

security

uac

Since Vista & windows 7 came out some of my .NET application has started throwing security exceptions.

I've noticed that some applications (i.e. my antivirus, control panel) have a small shield and when I run these applications administrator privileges are automatically requested from me by windows.

I know that as a user I can set the application to run as administrator but that's not good enough because if the application will run without privileges it would crash on my users machines.

Is there a way to tell windows (programmatically) I want the application to run with administrative privileges?

like image 980
Dror Helper Avatar asked Nov 23 '09 08:11

Dror Helper


1 Answers

Create an application manifest, set the requestedExecutionLevel to requireAdminstrator:

Example (generated by VS when you add Application Manifest):

<?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 Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
</asmv1:assembly>

If you add this to a Visual Studio application project, it will be embedded into your assembly when you compile.

like image 139
DSO Avatar answered Oct 29 '22 15:10

DSO