简体   繁体   English

如何在Asp .Net core 2.1中获取Ubuntu系统的当前用户?

[英]How to get the current user of the Ubuntu system in Asp .Net core 2.1?

I'm having a requirement to identify the current logged in user in Ubuntu system.我需要在 Ubuntu 系统中识别当前登录的用户。 In windows I can use在 Windows 中我可以使用

System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

to get the username.获取用户名。 I'm confused how to do that for Linux.我很困惑如何为 Linux 做到这一点。 Any suggestions?有什么建议?

I want the current logged in user in the controller.我想要控制器中的当前登录用户。

public ControllerName(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
            webRootPath = _hostingEnvironment.WebRootPath; 
            var user = User.FindFirst(ClaimTypes.Name).Value;/* System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); */
        }

But User is returning null always.User总是返回 null。

I solved the problem by executing terminal command whoami from the .Net core application. 我通过从.Net核心应用程序执行终端命令whoami解决了该问题。

You can refer the below links for some idea. 您可以参考以下链接获取一些想法。

1) run bash command from .NetCore with arguments 1) 从.NetCore运行带有参数的bash命令

2) https://loune.net/2017/06/running-shell-bash-commands-in-net-core/ 2) https://loune.net/2017/06/running-shell-bash-commands-in-net-core/

如果您只想要用户名

Environment.UserName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM