Node.Js - Get windows username

后端 未结 6 1544
臣服心动
臣服心动 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 compName = GetIPHost.HostName.ToString().Split('.').ToList();
            return compName.First();
        }
    

    MICROSOFT DOCUMENTATION

提交回复
热议问题