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

Mar*_*tin 12 c# .net-4.0 active-directory winforms

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

在此输入图像描述

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

mar*_*c_s 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;    
Run Code Online (Sandbox Code Playgroud)

新的S.DS.AM使得在AD中与用户和组玩游戏变得非常容易.


Lee*_*Lee 11

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

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
Run Code Online (Sandbox Code Playgroud)

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

  • 当我这样做时,我收到“类型为'System.DirectoryServices.AccountManagement.GroupPrincipal'的广播对象,类型为'System.DirectoryServices.AccountManagement.UserPrincipal'”错误... (2认同)