Node.Js - Get windows username

后端 未结 6 1474
臣服心动
臣服心动 2021-02-05 09:50

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

相关标签:
6条回答
  • 2021-02-05 09:54

    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

    0 讨论(0)
  • 2021-02-05 09:54

    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() );
    
    0 讨论(0)
  • 2021-02-05 09:57

    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.

    0 讨论(0)
  • 2021-02-05 10:05

    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

    0 讨论(0)
  • 2021-02-05 10:11

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

    0 讨论(0)
  • 2021-02-05 10:13

    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);
    
    0 讨论(0)
提交回复
热议问题