views:

3021

answers:

3

Hi everybody,

Is it possible to require administrator rights for one single method?

Something like this:

[RequireAdminRightsForThisMethod()]

private void TheMethod(){

    // Do something

}
+2  A: 

Take a gander here: http://www.omegacoder.com/?p=82

JonH
Malfist
This avoids the method if the applications does not have permissions. I need to launch the UAC to ask for administrator permissions.
lluismontero
+1  A: 

A method can require administrative privileges to run, but it's not possible to automatically elevate to Admin when executing a method.

SLaks
Why did this get downvoted?
0xA3
+12  A: 

You can add a PrincipalPermission attribute to your method to demand administrative privileges for its execution:

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

This is described in more detail in the following article:

Security Principles and Local Admin Rights in C# .Net

If you are looking for a way to elevate an already existing process I doubt that this is possible as administrator privileges are given on process-level to a process upon startup (see this related question). You would have to run your application "as administrator" to get the desired behavior.

However, there are some tricks that might allow you to do what you want, but be warned that this might open up severe security risks. See the following thread in the MSDN forums:

Launching MyElevatedCom Server without prompting Administrator credentialls from Standard User

Update (from comment)

It seems that if an update requires elevation your application update is best done by a separate process (either another executable, or your application called with a command line switch). For that 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();
0xA3
It is possible if you have credentials to change the thread to impersonate those admin credentials. I haven't done it in .NET, but I have in C++/Win32.
kenny
Funny I posted that same link...quite some time before you...
JonH
-1 for grabbing my link and answer and reposting it.
JonH
@kenny: Yes, that's surely true, but I doubt that it is possible to elevate the current process (which would be required for certain tasks). See the discussion here: http://social.msdn.microsoft.com/Forums/en-IE/windowscompatibility/thread/831ffd77-6bc4-4857-9947-d74923184b0b.
0xA3
Sorry, Jon, guess we both found the same source at the same time (its one of the first when you search the question title ;-). I had already written my answer when I saw your link. I don't care about the downvote, but personally I tend to downvote only incorrect answers.
0xA3
The reason I need that is because when the application has an update it needs to copy files to the application folder. I couln't do this without administrator rights. What I want is to fire the UAC when the "CoppyFiles()" method is called.Hope this helps to understand my question.
lluismontero
@lluis you know you could use the directoryservices object / namespace and check if the user is an administrator then implement the rest of the program...meaning if(admin)//do this else //dont do this
JonH
Thanks to everyone. I will restart the application asking for administrator permissions when I need to copy updated files.
lluismontero