Node.Js - 获取 Windows 用户名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27895127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:18:09  来源:igfitidea点击:

Node.Js - Getting windows username

node.jsactive-directory

提问by Benjamin

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

我正在尝试获取运行我的 node.jS 应用程序的机器的 Windows 用户名(该应用程序始终在 Windows 机器上运行)。

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

如何使用 Node.Js 获取当前的 Windows 用户名?

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

我正在尝试在 C# 中为 node.js 或CMD 中的whoamI命令找到类似于WindowsIdentity.GetCurrent().Name 的东西。

回答by Karthik M

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

从 Node v6 (2016-04) 开始,你可以只使用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);

回答by davidmdem

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.

我知道这不会按照 OP 的要求提供 Web 应用程序的客户端用户,但是在本地运行 Node 应用程序时尝试获取登录用户的搜索结果中这个问题非常高。

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

您可以在 Windows 中重现和使用环境变量的<domain>\<username>输出。whoamIWindowsIdentity.GetCurrent()

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

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

If you'd rather use USERPROFILE:

如果您更愿意使用USERPROFILE

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

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

回答by Rafael

Although node has the built in operating system function osand 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.

尽管 node 具有内置的操作系统功能osos.hostname()返回主机名的功能,但您将需要在 ASP.NET 或您选择的语言中访问客户端的主机名。您不能在节点中执行此操作,因为它在服务器端运行并且与客户端的本地信息无关。

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

Look at this question

看看这个问题

Determine Client's Computer Name

确定客户端的计算机名称

GET CLIENT HOST NAME IN ASP.NET AKA CLIENT SIDE

在 ASP.NET AKA 客户端获取客户端主机名

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

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

微软文档

回答by Jared

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

有人创建了一个名为username的模块,它为您完成所有艰苦的工作。

You use it like

你用它就像

var username = require('username');

console.log( username.sync() );

回答by Dawson

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.

我相信您是在说您有一个 Asp.NET 应用程序,并且想使用 Node.js 来确定当前用户的用户名,然后将其提交给您的 Asp.NET 应用程序。

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

我不在 Windows 上开发,但从这个问题我相信这可能存储为环境变量。process.env是环境变量的 javascript 映射/字典,可能包含用户用户名。

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

或者,您可以从用户主目录中解析它,如下所示:

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

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

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

我上面链接的问题意味着USERPROFILE解决C:\Users\USERNAME\. 然后我拆分路径并取第三个元素,即用户名。

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

同样,我没有 Windows 机器,无法确认这一点。希望这能让你走上正确的道路。

Cheers.

干杯。

回答by Gayathri Balasubramanium

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

从 Node v6 (2016-04) 开始,你可以只使用 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

一旦我们从上面的代码中获得了用户名。我们就无法通过客户端访问这个用户名 api。它显示了这个方法的 401 res