Node.Js - Getting windows username

五迷三道 提交于 2020-06-09 12:02:07

问题


I am trying to get the windows username of the machine that is running my node.jS app (the app is always running on a Windows machine).

How can I get the current windows username using Node.Js?

I am trying to find something similar to WindowsIdentity.GetCurrent().Name in C# for node.js, or whoamI command in CMD.


回答1:


Since Node v6 (2016-04), you can just use os.userInfo :

var os = require('os');
os.userInfo().username

To get the current logged-in username:

var path = require('path');
var userName = process.env['USERPROFILE'].split(path.sep)[2];
var loginId = path.join("domainName",userName);
console.log(loginId);



回答2:


I understand that this will not give the client user of a web app as OP asked, but this question is very high in the search results for trying to get the logged in user when running a Node application locally.

You can reproduce the <domain>\<username> output of whoamI and WindowsIdentity.GetCurrent() with environment variables in Windows.

process.env.USERDOMAIN + '\\' + process.env.USERNAME

If you'd rather use USERPROFILE:

process.env.USERDOMAIN + '\\' + process.env.USERPROFILE.split('\\').pop()




回答3:


Although node has the built in operating system function os and the os.hostname() to return the host name, you will need to access the client's hostname in ASP.NET or the language of your choice. You can't do that in node since it is running on the server side and has nothing to do with the client's local info.

> require('os')
> os.hostname()

Look at this question

Determine Client's Computer Name

GET CLIENT HOST NAME IN ASP.NET AKA CLIENT SIDE

System.Net.Dns.GetHostEntry( Request.ServerVariables["REMOTE_HOST"]).HostName;

SPOON FEED FOR THE LAZY

string IP = Request.UserHostName;
string compName = CompNameHelper.DetermineCompName(IP);

code from compnamehelper:

public static string DetermineCompName(string IP)
    {
        IPAddress myIP = IPAddress.Parse(IP);
        IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
        List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
        return compName.First();
    }

MICROSOFT DOCUMENTATION




回答4:


Someone has created a module called username that does all of the hard work for you.

You use it like

var username = require('username');

console.log( username.sync() );



回答5:


I believe you are stating that you have a Asp.NET application, and would like to use Node.js to determine the current users username, and then submit it to your Asp.NET application.

I do not develop on Windows though from this question I believe this may be stored as an environment variable. process.env is a javascript map / dict of environment variables and likely contains the users username.

Alternatively you can parse it from the users home directory as such :

var path = require('path'); var username = path.sep(process.env['USERPROFILE'])[2];

The question I linked above implies that USERPROFILE resolves to C:\Users\USERNAME\. I then split the path and take the 3rd element, being the username.

Again, I do not have a windows machine and could not confirm this. Hope this sets you down the right path though.

Cheers.




回答6:


Since Node v6 (2016-04), you can just use os.userInfo :

To get the current logged-in username:

var path = require('path');
var userName = process.env['USERPROFILE'].split(path.sep)[2];
var loginId = path.join("domainName",userName);
console.log(loginId);

once we get the username from above code.we can't able to hit this username api by client side.it showing 401 res for this method



来源:https://stackoverflow.com/questions/27895127/node-js-getting-windows-username

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!