Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement "As Administrator" or "Run As Administrator" in .NET application for selected operations?

I'm looking for correct way to allow elevating privileges for selected operations.

Something similar many products offer.

For example if you have UAC turned on and you are using Total Commander (running with basic privileges) when browsing restricted folders you will get:

enter image description here

If you push As Administrator you will get common User Access Control dialog asking if you want to grant the process required permissions. Is it something similar possible in .NET application (even with Win32 support)?

  1. How should be such functionality correctly implemented?
  2. Do I need to run external process to get elevated privileges just for selected functionality?

I'm not looking for solution which will demand running the main process as administrator.

like image 380
Ladislav Mrnka Avatar asked Jul 28 '11 13:07

Ladislav Mrnka


People also ask

How do I force my .NET application to run as administrator?

Net App To Run As Administrator. Go to Visual Studio Solution Explorer, right-click the project and select Add >> New Item…. In the Add New Item window, select Application Manifest File and press Add.

How do I run vs Run as administrator?

Run Visual Studio as an administratorOpen the Start menu, and scroll to Visual Studio. From the right-click or context menu of Visual Studio 2019 or Visual Studio 2022, select More > Run as administrator. When Visual Studio starts, (Administrator) appears after the product name in the title bar.

How could you run as administrator selected application from the Start menu?

Start context menu Open Start. Right-click the app (anywhere on the menu). Select the More submenu, and click the Run as administrator option.


1 Answers

You're going to have to factor the target operation functionality into a separate component that will be run elevated. This can be either an executable that requires elevation or a COM component (which could presumably be a COM wrapper for a .NET component). Details are available at http://msdn.microsoft.com/en-us/library/bb756990.aspx, with the When to Add the Shield Icon to Your Application's User Interface being particularly pertinent.

like image 88
Nicole Calinoiu Avatar answered Oct 13 '22 10:10

Nicole Calinoiu