Node.Js-Getting current windows username

I have an ASP.Net MVC project, and I am trying to get the client windows username (the client side is always running on Windows machine) and pass the windows username to the server side using Node.JS. 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.

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

Getting 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);

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.

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