简体   繁体   中英

How to know if node-webkit app is running with Administrator/elevated privilege?

如何确定 node-webkit 是否在 Windows 上以管理员权限运行?

On windows, you can use the npm package is-admin , to check if the node process is elevated.

const isAdmin = require('is-admin');

isAdmin().then(elevated => {
    if (elevated) {
        console.log('Elevated');
    } else {
        console.log('Not elevated');
    }
});

There is also a cross platform implementation called is-elevated which bundles elevation checks for Unix and Windows

Don't use npm packages for any small task - this is very bad practice.

    var isElevated;

    try {
        child_process.execFileSync( "net", ["session"], { "stdio": "ignore" } );

        isElevated = true;
    }
    catch ( e ) {
        isElevated = false;
    }

Using the node-windows module, you can call the following function to determine whether the current user has admin rights:

var wincmd = require('node-windows');

wincmd.isAdminUser(function(isAdmin){
  if (isAdmin) {
    console.log('The user has administrative privileges.');
  } else {
    console.log('NOT AN ADMIN');
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM