I'm thinking about developing an application that should run as standard user most of the time, but for certain operations, elevated administrative access will be required. The user expecience should be like in Windows Explorer when copying something into a protected folder.
Now the question is, how should this be implemented in .NET? I know that only entire processes or maybe some COM instances can be elevated, not single functions. But that would be exactly what I need. What path should I go? Write two executables, one with a manifest and the other without; programmatically run the same process elevated a second time; use some COM thing? Then I have the added privileges, but how to tell the other process what to do? Use .NET remoting (which is deprecated/complicated?); implement my own IPC thing using sockets/pipes/whatever? That elevated task may need to ask the user something in the middle of the process. And it must be cancellable.
There's a lot that tells me how UAC is working in the inside or how system administrators can configure it, but I haven't found anything that answers these basic questions.
We appear to have 2 distinct questions here:
Here's my attempt to answer them:
I would start by splitting out each of the actions that required elevated privileges to their own 'helper' programs. These helper programs would perform only a single action, taking arguments via the command line.
For example say your program needs to stop/start a service, I'd write a small helper program called servicecontroller (well in reality you'd probably want to use the net command) that took command line arguments similar to this:
servicecontroller stop MyCoolService
servicecontroller start MyCoolService
These arguments would be built up by the 'Main' program and passed along after hitting 'OK'.
There are several issues with the solution above though, that you may or may not care about:
A bit more googling revealed this blog post by 'DevZest' which basically recommends what I've described above. Good luck!
EDIT Based on the additional questions asked in the comments:
Without knowing exactly what you're doing here is my take on it:
In the file manager example I would do what explorer does and first spin though each of the destination directory to determine if any of the destination locations require elevated privileges to copy to. If they do I would mark a flag indicating that the action needs to be performed as an administrator, prompt the user and then perform the entire action as an administrator.
As to the text editor example, I would do something similar as above, when the user gives you a destination directory check to see that you have write access to that location, if not inform the user that they are saving to a location that requires elevation, save their work to a writable location (your program's APPDATA is probably a good place or even TEMP), then fire off the copy procedure which requests elevation, if the user cancels the UAC dialog make sure that you capture this cancel and delete the temporary file.
I would discourage you from keeping an elevated worker process around, if you find that your program needs to request elevation more than a few times (Readas: 1 or 2 corner cases) during the programs lifespan, then I would question why it wasn't marked as always elevated. I like to follow the rule of 'least surprise', when I gave you elevated rights I expect you to only perform the action that requested it, not to continue to pump actions that require elevation through the first request.
All of that being said there is nothing to prevent you from doing the above, once you let some process get elevated rights he can invite in all of his buddies. Based on your desire to keep an elevated process around why not just save the current state of the program, and relaunch the program with elevated privileges and restore the state? This, in my opinion, is the same as having the worker process exist in an always elevated state.
If you could tell us more about exactly what you're trying to do we might be able to find better ways to accomplish that, without having to run into UAC issues. While there are some very good reasons to write to areas protected by UAC, in most cases programs do not need to write/access to those locations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With