Administrator rights for one method

Is it possible to require administrator rights for one method?

Something like that:

[RequireAdminRightsForThisMethod()] private void TheMethod(){ // Do something } 
+60
c # permissions administrator
Jan 07 '10 at 16:24
source share
2 answers

You can add the PrincipalPermission attribute to your method to require administrative privileges to execute it:

 [PrincipalPermission(SecurityAction.Demand, Role = @"BUILTIN\Administrators")] public void MyMethod() { } 

This is described in more detail in the following article:

Security Principles and Local Administrator Rights in C # .Net

If you are looking for a way to raise an already existing process, I doubt that this is possible, since administrator rights are granted at the process level to the process at startup (see this related question ). You will need to run the application “as an administrator” to get the desired behavior.

However, there are some tricks that may allow you to do what you want, but be careful that this can cause serious security risks. See the following section on the MSDN forums:

Starting MyElevatedCom Server without prompting for administrator credentials from a standard user

Update (from comment)

It seems that if an upgrade requires upgrading, your application update is best done as a separate process (either by another executable file, or by your command line application). For this separate process, you can request elevation as follows:

 var psi = new ProcessStartInfo(); psi.FileName = "path to update.exe"; psi.Arguments = "arguments for update.exe"; psi.Verb = "runas"; var process = new Process(); process.StartInfo = psi; process.Start(); process.WaitForExit(); 
+80
Jan 07
source share

A method may require administrative privileges, but when you execute a method, it is not possible to automatically raise it to Admin.

+15
Jan 07 '10 at 16:27
source share



All Articles