10

Is there a way to check if an electron app is launched with the admin rights?

I only found electron-sudo lib to execute commands with admin privileges.

But I have multiple commands to execute and I do not want to prompt the user every time.

So how can I check if the app is started with admin privileges using electron ?

The best thing would be just to execute a command inside the software ex: .isAdminPrivilegesUsed (can be a script that is executed on Windows) that return true or false, and if false :

I will prompt the user that he has to restart the software with admin rights and close it

5 Answers 5

11

I checked into how to do this from Node and found this answer: How to know if node-webkit app is running with Administrator/elevated privilege?.

I checked into the answer, downloaded node-windows and tried it. The solution, however, brought up the UAC dialog and always responded with "The user has administrative privileges".

I dug into the node-windows code that handles the isAdminUser command and found that it tried to run NET SESSION and, if does not have privilege, tries to run it elevated causing the UAC dialog.

I pulled out the part that does the elevate and ended up with this snippet:

var exec = require('child_process').exec; 
exec('NET SESSION', function(err,so,se) {
      console.log(se.length === 0 ? "admin" : "not admin");
    });

I tested this by running the application normally and with "Run as Administrator". The code above correctly displayed "not admin" when not run as administrator and "admin" when run as administrator.

This should work for the content of your .isAdminPrivilegesUsed method you referenced in the question.

2
  • Thanks worked like a charm! I use this code to display a popup to force the user to restart the soft with admin rights
    – Aaleks
    Jun 7, 2016 at 10:03
  • It indeed works when i launch it from normal cmd, but did not in PowerShell =) Jul 28 at 6:39
9

You can now specify that an app should run with elevated privileges using the electron build tools:

electron-builder

Add the following to your package.json:

  "build": {
    "win": {
      "requestedExecutionLevel": "highestAvailable"
    }
  },

highestAvailable or requireAdministrator available. For full details, see: https://www.electron.build/configuration/win.html#WindowsConfiguration-requestedExecutionLevel

electron-packager

When you call electron-packager add the following command-line parameter:

--win32metadata.requested-execution-level=highestAvailable

highestAvailable or requireAdministrator available. For full details, see https://electron.github.io/electron-packager/master/interfaces/electronpackager.win32metadataoptions.html#requested_execution_level

Note

These options make the program request elevated privileges rather than check whether the program is running with administrator privileges.

3
  • A very worthy "Note" because basically what this does is updates manifest. Manifest could be updated one again with lower privileges ;)
    – Konstantin
    Nov 2, 2017 at 21:57
  • @NoobTW: I don't know anything about Windows Portable, but are you using a user with administrative privileges? Also try "requireAdministrator" instead of "highestAvailable".
    – DBolton
    Dec 27, 2017 at 19:01
  • 2
    That doesn't work for the latest version. See the issue github.com/electron-userland/electron-builder/issues/2936
    – Y. Tarion
    Aug 24, 2018 at 7:01
6

If you are using electron-packager, just add --win32metadata.requested-execution-level=requireAdministrator. Eg:

electron-packager app --asar=true --platform=win32 --arch=ia32 --win32metadata.requested-execution-level=requireAdministrator --overwrite

1
  • 1
    For electron-packager users, this answer was extremely helpful. The requireAdministrator option is technically in the docs somewhere, but not easy to find.
    – tscizzle
    Mar 18, 2021 at 19:39
2

A popular Electron app has a solution for this problem https://github.com/microsoft/vscode

In the package.json file they have two useful dependencies:

They check to see if permissions are elevated using native-is-elevated, and if not, prompt for an admin password using sudo-prompt.

You can read the source code for the process here: https://github.com/microsoft/vscode/blob/8845f89c1e4183b54126cd629cd45c8f0f7549f2/src/vs/platform/native/electron-main/nativeHostMainService.ts#L491

I have created an example Electron app using this approach here: https://github.com/kmturley/electron-runas-admin

1

Not a direct answer to your question. Another option to solve this problem is to force the application to be executed as administrator.

This can be done by updating the manifest file for the application, one guide on how to do this with Electron is here: http://layer0.authentise.com/electron-and-uac-on-windows.html

2
  • 1
    Thanks! I like this option !
    – Aaleks
    Jun 10, 2017 at 13:58
  • Please be warned that manifest could be updated one again with lower privileges ;)
    – Konstantin
    Nov 2, 2017 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.