如何获取当前登录用户的AD显示名称

时间:2011-07-14 11:57:33

标签: c# winforms .net-4.0 active-directory

考虑在Active Directory中为用户设置的以下属性:

enter image description here

在我的winforms应用程序中,我想显示当前登录并使用该应用程序的用户的显示名称。我该如何检索这些信息?

2 个答案:

答案 0 :(得分:26)

由于您使用的是.NET 4,因此可以使用System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里阅读所有相关内容:

基本上,您可以定义域上下文并轻松在AD中查找用户和/或组:

// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// find currently logged in user
UserPrincipal user = UserPrincipal.Current;

string displayName = user.DisplayName;    

新的S.DS.AM让您可以轻松地与AD中的用户和群组一起玩。

答案 1 :(得分:11)

经过几个小时寻找最简单的方法后,我终于遇到了这个

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

我想让更多像我这样的人去那里。