.net 获取 Windows 用户名 - 不同的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3175004/
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
Get Windows user name - different methods
提问by Andy
In .NET there appears to be several ways to get the current Windows user name. Three of which are:
在 .NET 中,似乎有几种方法可以获取当前的 Windows 用户名。其中三个是:
string name = WindowsIdentity.GetCurrent().Name;
or
或者
string name = Thread.CurrentPrincipal.Identity.Name;
or
或者
string name = Environment.UserName;
What's the difference, and why choose one method over the other? Are there any other ways?
有什么区别,为什么选择一种方法而不是另一种方法?还有其他方法吗?
采纳答案by sisve
Environment.UserName calls GetUserName within advapi32.dll. This means that if you're impersonating another user, this property will reflect that.
Environment.UserName 在 advapi32.dll 中调用 GetUserName。这意味着,如果您冒充另一个用户,此属性将反映这一点。
Thread.CurrentPrincipal has a setter and can be changed programmatically. (This is not impersonation btw.)
Thread.CurrentPrincipal 有一个 setter,可以通过编程进行更改。(顺便说一句,这不是冒充。)
WindowsIdentity is your current windowsidentity, if any. It will not necessarily reflect the user, think ASP.NET with FormsAuthentication. Then the WindowsIdentity will be the NT-service, but the FormsIdentity will be the logged in user. There's also a PassportIdentity, and you can build your own stuff to complicate things further.
WindowsIdentity 是您当前的Windows身份(如果有)。它不一定反映用户,认为 ASP.NET 与 FormsAuthentication。然后 WindowsIdentity 将是 NT 服务,但 FormsIdentity 将是登录用户。还有一个 PassportIdentity,您可以构建自己的东西,使事情进一步复杂化。
回答by Andreas Rejbrand
You asked for alternative ways.
你要求替代方法。
Of course, you can always use the native Windows API: GetUserName.
当然,您始终可以使用本机 Windows API:GetUserName。
回答by Maurizio In denmark
The three methods are described as follow:
这三种方法描述如下:
HttpContext = HttpContext.Current.User, which returns an IPrincipal object that contains security information for the current Web request. This is the authenticated Web client.
HttpContext = HttpContext.Current.User,它返回一个 IPrincipal 对象,其中包含当前 Web 请求的安全信息。这是经过身份验证的 Web 客户端。
WindowsIdentity = WindowsIdentity.GetCurrent(), which returns the identity of the security context of the currently executing Win32 thread.
WindowsIdentity = WindowsIdentity.GetCurrent(),它返回当前正在执行的 Win32 线程的安全上下文的标识。
Thread = Thread.CurrentPrincipal which returns the principal of the currently executing .NET thread which rides on top of the Win32 thread.
Thread = Thread.CurrentPrincipal 返回当前执行的 .NET 线程的主体,该线程位于 Win32 线程之上。
And they change in result depending on your IIS configuration as explained in this article: http://msdn.microsoft.com/en-us/library/aa302377.aspx
它们的结果会根据您的 IIS 配置而改变,如本文所述:http: //msdn.microsoft.com/en-us/library/aa302377.aspx
回答by Pavel Radzivilovsky
I believe the property was put in several places so that it would be easier for the programmer to find. There's only one logged in user, and only one respective name.
我相信这个属性被放在了几个地方,这样程序员会更容易找到。只有一个登录用户,并且只有一个各自的名称。

