Forced the application to have administrator rights

I need to grant administrator privileges for the application, knowing that it will be launched from the user session, and not from the administrator account.

I looked at other sites, but can not find anything that will help.

I tried editing the manifest by the way and inserted the line:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> 

This gave me an error while trying to publish using ClickOnce, but not when debugging.

Can you help me?

+5
source share
2 answers

first of all - indeed, it is not allowed by the design to install and use the ClickOnce application as an administrator: http://msdn.microsoft.com/en-us/library/142dbbz4(v=vs.90).aspx

Take a look at this post: http://antscode.blogspot.ca/2011/02/running-clickonce-application-as.html - it explains how to launch the ClickOnce application as an administrator. BUT - he must say that I followed this path - and I was not lucky with him. I had numerous problems with this approach (trying to run the ClickOnce application as administrator). As far as I remember, the biggest problem was that automatic updating did not work properly. Not to mention that non-admin users may need to enter administrator credentials all the time.

Thus, I would advise rethinking your logic and encapsulating the part that needs to be done as an administrator in a separate EXE file , and to make it very understandable for the user, when he presses the THAT button, a UAC prompt will appear (maybe add a shield icon to the button ) And in this button_click event do something like this:

 // 2. Run Photoshop action on this image (and wait for the task to complete) if (string.IsNullOrWhiteSpace(this.PhotoshopEnhanceActionAbsPath) == false) { var pi = new ProcessStartInfo(this.PhotoshopEnhanceActionAbsPath, "\"" + imgPhotoshopActionAbsPath + "\""); pi.UseShellExecute = true; pi.Verb = "runas"; var photoshopAction = Process.Start(pi); var success = photoshopAction.WaitForExit(); if (success == false) { // do something here } } 

this approach worked very well for me. The key is here:

 pi.UseShellExecute = true; pi.Verb = "runas"; 

it starts your EXE with administrator rights, so at that moment the UAC prompt will be displayed. Another nice consequence here is that users may not run this particular logic every time they use the application - and therefore they will not be annoyed with a hint when they do not need them.

+5
source

I am sure this is a design behavior.

ClickOnce applications are designed to be installed without administrator rights. It is impossible to raise them at runtime, since this means that effectively a user who does not have administrator rights can install and then run the application as an administrator; it will be a security risk.

+1
source

Source: https://habr.com/ru/post/915729/


All Articles