获取logedin windows用户的名字?

我怎样才能得到我的名字姓在我的系统中的C#(使用Active Directory用户名和密码login窗口)?

是否有可能做到这一点,而不去公元?

如果你使用的是.Net 3.0或更高版本,有一个可爱的库,这使得它实际上写自己。 System.DirectoryServices.AccountManagement拥有一个UserPrincipal对象,可以准确获取所需的内容,而且不必混淆LDAP或放弃执行系统调用。 这就是所有这一切:

Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal; // or, if you're in Asp.Net with windows authentication you can use: // WindowsPrincipal principal = (WindowsPrincipal)User; using (PrincipalContext pc = new PrincipalContext(ContextType.Domain)) { UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name); return up.DisplayName; // or return up.GivenName + " " + up.Surname; } 

注意:如果您已经拥有用户名,则实际上并不需要委托人,但是如果您在用户环境下运行,那么从这里拉出来同样容易。

有一个更简单的方法来做到这一点:

 using System.DirectoryServices.AccountManagement; UserPrincipal userPrincipal = UserPrincipal.Current; String name = userPrincipal.DisplayName; 

在这里输入图像说明

这个解决scheme对我来说不起作用,但是这个function很好用:

 public static string GetUserFullName(string domain, string userName) { DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + domain + "/" + userName + ",User"); return (string)userEntry.Properties["fullname"].Value; } 

你应该这样称呼它:

 GetUserFullName(Environment.UserDomainName, Environment.UserName); 

( 在这里find)。

检查这个职位:

如何在Windows中获取login用户的全名?

也许它有帮助。 转到这个答案:

如何在Windows中获取login用户的全名?

接受的答案也很有趣。