Saturday, April 20, 2013

Running .net application as an Administrator using UAC

You probably are already familiar with the whole new concept of UAC(User Access Control) security model that Microsoft introduced since windows Vista. This security model restricts the application from making system level changes in your application and requires any application that need such kind of access control to go through UAC dialog for user's consent. User can then authorize or simply cancel the application execution.


My purpose here is not to go in-depth regarding UAC, however as an application developer building a desktop app that requires administrative privilage to make system changes, you and I both are interested in popping up this UAC dialog to run the application with Administrator privilege so that your application do not fail with security reason. Please remember that if your application do not perform any Administrative task then do not show this dialog for fun as It might irritate your user and this is something you always need to be careful about as a developer.

The solution is rather simple and shorter than all the bullshits that I bothered you with, above.

  1. Add "Application Manifest File" in your application using Add New Item Dialog.
  2. Find <requestedExecutionLevel /> element inside the manifest file. This is the only element in the whole manifest file we are interested in right now.
  3. If you have gotten this far and have properly read the comment inside the manifest file then you already know what to do next.
  4. If not change/add level attribute in this element to "requireAdministrator". By default it is "asInvoker".
  5. Now compile your application and run the exe inside "bin" folder. 
  6. After the changes your <requestedExecutionLevel /> element should look something like this <requestedExecutionLevel level="asInvoker" uiAccess="false" />
If you are looking for a demo then you can download the sample code from here.



No comments:

Post a Comment