如何使用不同的方法获取Windows用户名?

编程入门 行业动态 更新时间:2024-10-11 09:28:24
本文介绍了如何使用不同的方法获取Windows用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在.NET中,似乎有几种方法可以获取当前的Windows用户名.其中三个是:

In .NET, there appears to be several ways to get the current Windows user name. Three of which are:

string name = WindowsIdentity.GetCurrent().Name;

string name = Thread.CurrentPrincipal.Identity.Name;

string name = Environment.UserName;

有什么区别,为什么选择一种方法而不是另一种方法?还有其他方法吗?

What's the difference, and why choose one method over the other? Are there any other ways?

推荐答案

Environment.UserName调用advapi32.dll中的GetUserName.这意味着,如果您要冒充其他用户,则此属性将反映出来.

Environment.UserName calls GetUserName within advapi32.dll. This means that if you're impersonating another user, this property will reflect that.

Thread.CurrentPrincipal有一个setter,可以通过编程方式进行更改.(顺便说一下,这不是假冒.)

Thread.CurrentPrincipal has a setter and can be changed programmatically. (This is not impersonation btw.)

WindowsIdentity是您当前的 windows 身份(如果有).它不一定反映用户,认为带有FormsAuthentication的ASP.NET.然后WindowsIdentity将成为NT服务,而FormsIdentity将成为登录用户.还有一个PassportIdentity,您可以构建自己的东西来进一步使事情复杂化.

WindowsIdentity is your current windows identity, 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.

更多推荐

如何使用不同的方法获取Windows用户名?

本文发布于:2023-11-07 02:07:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   用户名   方法   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!