简体   繁体   English

在.NET Core Console中获取当前用户

[英]Getting Current User in .NET Core Console

In a .NET Core console application (note: not ASP.NET Core!), how can I get the current user? 在.NET Core控制台应用程序中(注意:不是ASP.NET Core!),如何获取当前用户? To be clear, I'm looking for what used to be available as Thread.CurrentPrincipal, which no longer exists. 为了清楚起见,我正在寻找以前不再存在的Thread.CurrentPrincipal。 PlatformServices does not contain this information, and neither does Environment. PlatformServices不包含此信息,Environment也不包含此信息。

Got it. 得到它了。 A possible option is to use WindowsIdentity: 可能的选择是使用WindowsIdentity:

WindowsIdentity.GetCurrent().Name

It is necessary to add the System.Security.Principal.Windows package. 有必要添加System.Security.Principal.Windows包。 Of course, this is for Windows only. 当然,这仅适用于Windows。

Another option is to use Claims: 另一种选择是使用声明:

ClaimsPrincipal.Current

For that, the package to add is System.Security.Claims. 为此,要添加的包是System.Security.Claims。 In Windows, by default, the identity will be empty. 在Windows中,默认情况下,标识将为空。

System.Security.Principal.Windows is not available unless you import the DLL manually. 除非您手动导入DLL,否则System.Security.Principal.Windows不可用。 The following worked for me: 以下对我有用:

Environment.UserName;

According to the .NET Core System.Environment Source Code , this solution "should suffice in 99% of cases." 根据.NET Core System.Environment源代码 ,这个解决方案“应该足够99%的情况。”

Note: Be sure you are targeting DotNetCore 2.0 or later as 1.0 and 1.1 do not have this definition. 注意:请确保您的目标是DotNetCore 2.0或更高版本,因为1.01.1没有此定义。

Edit Jan 22 2019: The link to the old source of the method has gone stale. 编辑2019年1月22日:该方法的旧源的链接已过时。 PR 34654 moved System.Environment to live in CoreLib . PR 34654System.Environment移动到CoreLib For those curious to read the source code , you may still do so, though the comment suggesting it "should suffice in 99% of cases" has been removed. 对于那些好奇的阅读源代码的人来说 ,你可能仍然会这样做,尽管评论表明它“应该足够99%的情况”已被删除。

If you want to reuse IIdentity abstraction to pass through your middle layer do this: 如果要重用IIdentity抽象来传递中间层,请执行以下操作:

var identity = new GenericIdentity(Environment.UserDomainName + "\\" + Environment.UserName, "Anonymous");

PS in core 2 console app: ClaimsPrincipal.Current and Thread.CurrentPrincipal are always null (unless you have setup them) and this code will not work either: 核心2控制台应用程序中的PS: ClaimsPrincipal.CurrentThread.CurrentPrincipal始终为null(除非您已设置它们),此代码也不起作用:

IPrincipal principal = new GenericPrincipal(identity, null);
AppDomain.CurrentDomain.SetThreadPrincipal(principal);

after this ClaimsPrincipal.Current and Thread.CurrentPrincipal are still null. 在此ClaimsPrincipal.CurrentThread.CurrentPrincipal仍为null之后。

WindowsIdentity.GetCurrent() works, but there should be more strong reasons to reference System.Security.Principal.Window then get the "user name". WindowsIdentity.GetCurrent()有效,但应该有更强大的理由来引用System.Security.Principal.Window然后获取“用户名”。

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

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