如何在Asp.Net core 2.1中获取Ubuntu系统的当前用户?
How to get the current user of the Ubuntu system in Asp .Net core 2.1?
我需要识别 Ubuntu 系统中的当前登录用户。在 windows 我可以使用
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
获取用户名。我很困惑如何为 Linux 做到这一点。有什么建议吗?
我想要控制器中当前登录的用户。
public ControllerName(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
webRootPath = _hostingEnvironment.WebRootPath;
var user = User.FindFirst(ClaimTypes.Name).Value;/* System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); */
}
但是 User
总是返回 null。
我通过从 .Net 核心应用程序执行终端命令 whoami
解决了这个问题。
您可以参考以下链接了解一些想法。
1)run bash command from .NetCore with arguments
2)https://loune.net/2017/06/running-shell-bash-commands-in-net-core/
如果您只需要用户 姓名:
Environment.UserName
我需要识别 Ubuntu 系统中的当前登录用户。在 windows 我可以使用
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
获取用户名。我很困惑如何为 Linux 做到这一点。有什么建议吗?
我想要控制器中当前登录的用户。
public ControllerName(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
webRootPath = _hostingEnvironment.WebRootPath;
var user = User.FindFirst(ClaimTypes.Name).Value;/* System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString(); */
}
但是 User
总是返回 null。
我通过从 .Net 核心应用程序执行终端命令 whoami
解决了这个问题。
您可以参考以下链接了解一些想法。
1)run bash command from .NetCore with arguments
2)https://loune.net/2017/06/running-shell-bash-commands-in-net-core/
如果您只需要用户 姓名:
Environment.UserName