Accept UAC Prompt Programmatically - c#

I have a C# application that needs to run with administrative permissions. In particular, it loads at Windows 7 start-up and executes a few scripts in the background. This process is fully invisible to the user and is covered up with a background when executing. The permissions implementation is via a manifest file. Now, the problem is that I need to automate the step where UAC prompt comes up, and invisibly accept it.
How would something like that be done in C#?

I think what you have to do is impersonate a user with administrator privileges.
Try this: http://msdn.microsoft.com/en-us/library/chf6fbt4.aspx
I have had success with this method before, give it a shot!

Related

How to force a prompt for credentials when launching a program and UAC is disabled?

Background
My application (.NET, C#) performs an in-app upgrade. Depending on installation configuration, the upgrade may need to launch a separate process that requires Administrator privileges. The launched program's manifest specifies that it requires Administrator privileges.
This normally works fine, but if UAC is disabled, and the user is not an admin, then it fails miserably. See below.
Scenarios
UAC is enabled, and the user is an admin - Windows prompts for elevation, and the program executes successfully.
UAC is enabled, and the user is not an admin - Windows prompts for credentials, and the program executes successfully.
UAC is disabled, and the user is an admin - No prompts, the program executes successfully.
UAC is disabled, and the user is not an admin - No prompts, the program starts but fails miserably.
This fourth scenario is the one that I want to solve. I have a rather large customer company that has a policy of turning off UAC on all corporate PCs, for whatever reason.
Mitigation
The simplest solution is to detect this fourth scenario and give the user guidance. I have no problem detecting when this fourth scenario is in effect, and I can then open up an Explorer window to a directory containing the program to execute, and a text file that walks the user through the process of Shift-Right-Click -> "Run as different user".
Desired solution
If I detect that I am in this fourth scenario, then I perform some coding magic to automatically start the process from my application AS IF the user had manually opted to "Run as different user".
I don't know how to implement this solution. I tried to set ProcessStartInfo.Verb = "runas", as I have seen suggested, but that doesn't seem to do anything useful in the absence of UAC. Any ideas?
The correct verb for this case would be
ProcessStartInfo.Verb = "runasuser"
It works well to trigger the "run as different user" authentication dialog for another process.
One possible downside I found was that you will not get a proper process object returned, so you get no handle for that new process. Also it is not really possible to restart your own application with this method as the authentication dialog is running as a thread of the spawning application and only after authentication is a new process created. So if you want to "restart" your application a launcher or some other trick would probably be needed.

Will not being able to access CMD affect my program?

I am writing a piece of software that needs to launch another program. On the network I am using I am not an administrator and command prompt is blocked.
Will Process.Start() still work?
Will Process.Start() still work?
I can't see how an admin can prevent one process from spawning a child process. It's an essential part of Windows.
For example:
Windows Explorer runs in the user account of the currently logged-in user. In Windows 7+, selecting any folder with the mouse and pressing ctrl+enter, causes a new Windows Explorer window to appear for the selected folder in a new process! You can verify this in Task Manager
If you can do that on your computer in the selected user account, so too should your application.
Blocking the command prompt will not stop Process.Start() from working.
How ever if an administrator has gone to the trouble to block the command prompt they may have implemented a group policy to only allow certain programs to be run.
See this link for details http://www.nextofwindows.com/how-to-use-local-group-policy-whitelist-certain-programs-in-windows-7
You may not be able to start any programs that are not on this "whitelist".
The easiest way to find out is to just try it yourself, but if you are able to run your own custom program that calls Process.Start(), the fact that your program runs, probably means the administrator has not set up such group policy.

How does Chrome update without UAC

I just try to understand the Google update service (aka Omaha) and can't find any information about how Omaha updates Chrome without the UAC Prompt?! Or without any Administrator privileges.
I am looking for a way to lunch a update service that can Update a Application in the Program folder (without UAC / Administrator privileges) just like Omaha does, but i can't find any documentation regarding that topic. Tried browsing the source code a bit thought but i couldn't find anything that helps.
In fact only the Installation should require Administrator Rights, but the update should not.
Any "easy" way to achieve this in C#?
Chrome installs a Windows Service, which is always running and preforms the update. The service runs as SYSTEM and so can perform the update without UAC prompt.
See:
https://support.google.com/installer/answer/98805?hl=en
http://omaha.googlecode.com/svn/wiki/OmahaOverview.html (Execution model section)

c# starting process with lowered privileges from UAC admin level process

I have one major problem with my app. I have an app & updater as a separate exe files. When an update is available, updater does the update, and on process completion it starts my app. The main problem is - app is installed in program files folder, so updater need UAC admin privileges, and that's ok, but when I need to run my app updater needs to run it as a normal user, because if it's run as an administrator drag and drop doesn't work (not an app problem, UAC blocks it). I've tried several different solutions, and even this one:
How to run NOT elevated in Vista (.NET)
It haven't helped me - my app is run as an administrator.
You'd better avoid starting a non-elevated process from an elevated one. It's tricky part and error-prone.
This approach is better:
Your updater initially starts as non-elevated application, and its manifest has asInvoker level.
When it starts, it restarts itself with elevated privileges using runas verb, and passes a command-line parameter to indicate it. This instance performs the update and returns.
Here comes the non-elevated updater again, and starts your application with the same non-elevated user token that started the first instance of updater in step 1.
Pretty simple and robust.
Look at this post on how to Enable Drag and Drop for an Elevated process. Even though it says MFC app you can cll those Windows API in Any app I suppose
https://helgeklein.com/blog/2010/03/how-to-enable-drag-and-drop-for-an-elevated-mfc-application-on-vistawindows-7/

How to call a Win32 API with elevated priviledges on Windows 7

We have just discovered that code that calls the Win32 SetDateTime function needs to run in elevated mode in Windows 7, i.e. even when logged in as an administrator, one still has to choose to run the Set Date code as Administrator for the call to change the date.
Is there anything I can do in code to always call this function under elevated privileges?
You can use the ShellExecute API call to launch an executable with elevated privileges.
However, if UAC (user access control) is enabled on the machine, the user will still get the UAC prompt asking for permissions to run elevated.
So I don't think you can avoid the prompt for elevation permission, but at least your user doesn't have to manually run the program as an admin.
Please let me know if you need help in how to launch an executable as an admin.
This is just not how security works. Changing the clock is a very intrusive operation, it has a very large number of side effects. There is no mechanism in Windows, or any other operating system for that matter, where you could start a process with limited privileges and then just arbitrarily bypass these limitations and suddenly gain administrator rights. There wouldn't be any point whatsoever to running programs with limited privileges if that was possible.
If you want to do something like this then you'll have to run your program with elevated rights. On Vista and Win7 that requires you to run as a service or a scheduled task. Which require an administrator to get installed. UAC provides a way gain admin rights for regular programs, you have to include a manifest in your program so the user is notified about your privilege elevation. Google 'requireadministrator', take the first hit.
Like others have said, you need to spawn a new process to get elevated permissions, which will result in a UAC dialog.
If this is something you need to run unattended you could try running as a service. That would give you the elevated context you need.

Categories