23

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.

Benjamin
  • 3,499
  • 8
  • 44
  • 77
  • It wasn't first clear here when you are using the term windows you were referring to the operating system. – Dawson Jan 12 '15 at 04:09
  • The current Windows username is not what you want (that would be the user running the server process). You need NTLM authentication. – SLaks Jan 12 '15 at 04:11
  • 1
    Since Nodejs v6, one can use `os.userInfo().username` See [os.userInfo](https://nodejs.org/dist/v6.0.0/docs/api/os.html#os_os_userinfo_options) – Todd Jul 23 '18 at 15:43

6 Answers6

43

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);
R3uK
  • 14,417
  • 7
  • 43
  • 77
Karthik M
  • 522
  • 5
  • 3
8

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()

davidmdem
  • 3,763
  • 2
  • 30
  • 33
4

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

Community
  • 1
  • 1
Rafael
  • 7,605
  • 13
  • 31
  • 46
  • 2
    Down voted because you seem to have over looked what is being asked. He is looking to get the current users username. – Dawson Jan 12 '15 at 04:21
  • 1
    Down vote removed. I assumed he would be having his clients run node... I don't seem to understand the use case he is going for. – Dawson Jan 12 '15 at 04:27
  • Thanks for the reply, however Os.hostname returns the computer name, not the userID in Active Directory. – Benjamin Jan 12 '15 at 05:14
3

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() );
Jared
  • 2,978
  • 4
  • 26
  • 45
2

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.

Community
  • 1
  • 1
Dawson
  • 4,391
  • 2
  • 24
  • 33
-2

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