我如何获得当前用户名在。net使用c# ?


当前回答

我从已有的答案中尝试了几种组合,但它们给了我

DefaultAppPool
IIS APPPOOL
IIS APPPOOL\DefaultAppPool

最后我用了

string vUserName = User.Identity.Name;

这给了我实际的用户域用户名。

其他回答

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

试试这个属性:Environment.UserName。

如果你在一个用户网络中,那么用户名将是不同的:

Environment.UserName
- Will Display format : 'Username'

而不是

System.Security.Principal.WindowsIdentity.GetCurrent().Name
- Will Display format : 'NetworkName\Username'

选择您想要的格式。

你可能还想尝试使用:

Environment.UserName;

像这样…:

string j = "Your WindowsXP Account Name is: " + Environment.UserName;

希望这对你有所帮助。

以防有人在找用户显示名而不是用户名,比如我。

下面是奖励:

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName

在项目中添加对System.DirectoryServices.AccountManagement的引用。