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

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

在此处输入图像描述

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

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

  • 管理.NET Framework 3.5中的目录安全性主体
  • System.DirectoryServices.AccountManagement上的MSDN文档

基本上,您可以定义域上下文并在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中使用用户和群组变得非常容易。

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

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

对于像我这样的更多人,我想把它拿出来。