Elevation without restarting an application? - c#

Has anyone managed to get administration rights through the UAC without restarting the application or embedding a manifest file?
I'd like to write to some files that only administrators can modify, without relying to another elevated application. Is it possible to impersonate an administrator previously calling with some native API the UAC prompt?
I guess this is not possible and I'll have to use an external tool with elevated rights, but I'm asking just in case.
EDIT: I know there are some other similar questions around, but since they do not cover the topic of impersonation (as fas as I've seen), nor some possible native call to the UAC prompt I decided to give a new thread a go...

As stated in this other question, it is not possible, you have can eleveate COM object or another process, but not the current process.
Request Windows Vista UAC elevation if path is protected?

I read that thread (along with a lot more heh), but you never know what other people has managed to do recently. And maybe the release of SP2 changed something, dunno.

Well, I guess there are no changes on how the UAC works nor any discovery on workarounds, so I'll mark the previous answer as the right one.
I'll make another application that performs the administrative tasks I need.
Thank you.

If all you want to do it get admin rights without a manifest (ie: im assuming you're not talking about cracking/hacking the UAC implementation), then just create a shortcut with the "Run As Admin" checkbox checked. You can programmatically generate this shortcut as long as you look up how short cuts are written (in terms of their fiel contents) and I'm sure there is some name/value pair you will need to include inside the shortcut to ensure that Run As Admin is checked by default. Now, when people run that shortcut instead of your exe, they will get prompted to run your exe in elevated mode (with admin rights), and this way you will not need to restart or use a manifest.
Excuse me if I have misunderstood your question.

Adding a manifest file in the same directory as the executable "works for me". Not actually getting the permissions, but it prompts for the permission.

Related

Start process as normal user from ClickOnce application

We have a ClickOnce application that starts an external process with
p = new Process();
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(exe);
p.StartInfo.FileName = Path.GetFileName(exe);
p.StartInfo.Arguments = arguments;
p.Start();
Usually, people ask whether it is possible to start the process as administration. But in my case it is the opposite:
I would like to make sure that this application is started as the user that is logged on. By default, that happens - for example when I test it.
But some users (Windows 8.1) report that the exe is started as administrator. For some weird reason, Windows automatically wants to start it as admin, even if it would not be necessary at all. If they right click on the exe and check the Properties, the box Run this program as an administrator is not checked!
When checking the exe file in the folder, it shows a UAC icon (example below):
Now I am wondering if it is programmatically possible to prevent that behavior and start the process in non-elevated state, maybe with a StartInfo property?
If application requires UAC, there are two reasons:
There is a manifest (embedded into executable, or external one) which requires that.
Windows decided, based on certain conditions, that application needs evelated permissions.
Since as you say in your question, the same executable does not require UAC on certain machines while does require on others - most likely there is no embedded manifest and that is Windows who decides this application needs evelation.
Here you can find somewhat dated, but I hope still relevant architecture of UAC. Under "Installer detection technology" you will see that even simple things like "The file name includes keywords such as "install," "setup," or "update." might cause windows to force UAC on that executable. First check if this somehow helps you to solve your problem (like maybe on some machines executable matches some of those criterias).
If that does not help, and because embedded manifest is unlikely - you can try to create external manifest for that exe (you can do this automatically before starting process) in which you request requestedExecutionLevel asInvoker. Put that manifest (it should be named .exe.manifest) near problem exe (not your clickonce application) and see if that helps. Hope you know how to create manifest file, if not - you can easily find that on Google or ask here. Note that if executable has embedded manifest already - it will have a priority (but that is unlikely).
Maybe this will help you?
launch C# exe without prompting UAC on Windows 7
Add this to your manifest:
requestedExecutionLevel level="asInvoker" uiAccess="false"
"It could be that your third party DLL has to run in elevated mode, so your best option is to run as administrator. Bypassing the UAC prompt without running as administrator is a long complicated process"
You're doing it right by not specifying any account information when creating a new Process instance: that'll rely on the default behavior of using the same user account running current process.
As for some users getting the "Run As Administrator" prompt:
it may be that the security rules on the machine somehow identified the executable as not safe. Try to ask your customer to check whether in the properties page of the app they see an "Unblock" button. If so, let them click the "Unblock". That will eliminate that UAC prompt in the future.

Possible way to implement client update application without UAC prompt

I need to develop an application that will run on clients. The main focus is to let the application update another -specific- application and be able to monitor own defined states for clients. I'm considering something like a console application with TopShelf because it makes debug a lot easier. However... to install or update this specific application I must use a MSI. But if for example I create a process that executes msiexec the client/end user will be prompt with UAC. This is a big issue because clients should be updated unattended. And UAC must stay the way it is : highest level.
I've read a lot of questions/answer concerning UAC prompt... and to be honest I'm starting to doubt my own approach. I was not able to find any question that I could relate to.. so I would appreciate it if someone could give me some good advise, tips, references etc on a possible way to approach this issue.
Thanks in advance
Create a Task Scheduler job with the "Run with highest privileges" option checked.
You can easily create this job programatically, even using a XML configuration file.
This way the user is prompted to allow UAC only once (when first clicking an exe for example).

Does a C# console app run under current user account?

Before I open my big mouth and say it is the default way a console app works in c# I wanted to get a few of you folks opinion.
Essentially, we have a c# console app the create a folder full of files from byte streams. Pretty simple. They now want to have it write directly to a shared drive that only a few people have read/write privilages.
I think that as long as they kick of the exe while logged in as as privelaged user it will copy to the share drive.
Unfortunately, they want this information without creating the folder and share first. sigh b/c that would have been too easy to just test the blasted thing.
To answer the question in the title of your question, yes, a C# console app runs under whatever account you run it as.
Note, you should be able to right click on the executable and click "run as" or "run as administrator" instead of actually logging on as another user.
I hope I'm not missing the point of your question.
By default, yes, it will run under the logged in user credentials (as would any other application).
Of course, if this is run as a scheduled task, uses the Process class or the runas command, one can specify a different user.
If you open task manager, you will see the process owner in the "User Name" collumn.
Any application run without specifying a specific user (Run as...) runs under default logged in user's context.

C# setup project output automatically start with administrative rights

i've created an setup project for a .net-application which works fine. The problem is, that the application begins to write log-files after startup and this requires administrative rights on windows vista and windows 7. I know there are some folders which i could use to write into without administrative rights. When i start the application as administrator, everything works fine.
I asked myself, if it's possible to change the setup project in visual studio in a way that the installed application automatically owns administrative rights on the target system?
Thanks for every help in advance!
Alex
You don't want to do this. If it needs rights to a particular directory, then create an account that has them (or modify the subdirectory's rights so that anyone can access it). But don't run as admin just for one feature. Also, look into whether there's another directory you could be writing to, which you already have rights to.
To answer your direct question, yes you can setup a launch condition for the setup project to require administrator, I believe this will take care of the proper escalation for you.
here is a thread with detailed instructions.
Now, to expand, I agree with Steven that you SHOULDN'T do this, at least not just to be able to write a log file.....
You could technically do this with a bad hack: The idea is to have the Setup program which runs elevated create a scheduled task that runs the program as admin.
Instead of directly creating a shortcut to your application, the setup creates an shortcut to execute this scheduled task.
See this as reference on how to do that manually: http://www.howtogeek.com/howto/windows-vista/create-administrator-mode-shortcuts-without-uac-prompts-in-windows-vista/
You just need to find a way to create the task programatically from your setup.
On the other hand, I totally agree with Steven too. Try to do it the 'right' way and don't trick the Windows security. It's a good thing and working around it makes the system potentially unsecure, and you don't want to be the one compromising tht Security of others computers.

automatically update my .NET application in Vista without admin rights

I know there have been a ton of questions about how to implement automatic updates in a .NET application, and I've probably read most of them. Unfortunately, none I have seen quite fit my situation as far as I can tell.
I am trying to build a self-updating .NET application that will run in an environment with limited user accounts. No admin rights here. The application must run on Vista and XP, and probably Win 7 when it comes out. An X-copy deployment would be fine. I would prefer NOT to use click-once
So far I have tried a system where the application checks a manifest and downloads updated binaries, which worked great in development, but not so much when the app was installed. Vista refuses to to let me copy files to Program Files. After that, I tried downloading an updated .msi, which works except that the MSI has a UAC prompt -- no go if the user does not have an account with local admin privileges.
Some posts have suggested that running the program in AppData and using the x-copy method might work, but I have not seen anyone say that they have tried it and it did work. Will that work for an account without admin rights? Can anyone vouch for this method before I start writing more code?
You do just need to make sure that you're 'installing' (i.e. copying to) a location which non-privileged users have access to.
There's nothing magic about Program Files except that a non-privileged user can't write to it (by default).
If you have an admin do your initial install, then you could change the permissions on your program files folder so that subsequent upgrades could overwrite those files. But that's not really in the spirit of things.
The main disadvantages of ClickOnce are the uncommon install location and the primitive installer - if you're going to be doing xcopy to somewhere weird anyway, then you already have much of what's already icky about ClickOnce, without any of the advantages.
Although fairly complex, one strategy which will work is to create a windows service which runs as local system. Install it once as an administrative user, then have this service download your updates and copy them to the program files directory.
You'll probably also need to make some sort of user interface to display progress and status during the update process. Fairly easy to expose it via a WCF service, or the like.

Categories