C# – 在活动目录中查找用户管理器

开始使用System.DirectoryServices.AccountManagement命名空间,对Active Directory(AD)中的用户执行查找。 我还需要用户的经理 ,但我似乎在使用这个命名空间遇到了麻烦。 获取一个人的当前代码:

 class Person { // Fields public string GivenName = null; public string Surname = null; public string DistinguishedName = null; public string Email = null; public string MangerDistinguishedName = null; // Unable to set this // Constructor public Person(string userName) { UserPrincipal user = null; try { user = GetUser(userName); if (user != null) { this.GivenName = user.GivenName; this.Surname = user.Surname; this.DistinguishedName = user.DistinguishedName; this.Email = user.EmailAddress; this.MangerDistinguishedName = user. } else { throw new MissingPersonException("Person not found"); } } catch (MissingPersonException ex) { MessageBox.Show( ex.Message , ex.reason , MessageBoxButtons.OK , MessageBoxIcon.Error ); } catch (Exception ex) { MessageBox.Show( ex.Message , "Error: Possible connection failure, or permissions failure to search for the username provided." , MessageBoxButtons.OK , MessageBoxIcon.Error ); } finally { user.Dispose(); } } 

执行搜索此人

  private UserPrincipal GetUser(string userName) { PrincipalContext ctx = new PrincipalContext(ContextType.Domain); UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName); return user; } 

直接访问特定用户的经理的专有名称的另一种方法是什么?

  • 这可能是VB中的部分答案,但我没有看到提到经理。
  • 这里的另一个可能的部分原因是管理者。

如果您使用的是.NET 3.5及更高版本且使用System.DirectoryServices.AccountManagement (S.DS.AM)命名空间,则可以轻松扩展现有的UserPrincipal类以获取更多高级属性,例如Manager等。

在这里阅读所有相关内容:

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

基本上,您只需基于UserPrincipal定义派生类,然后定义所需的其他属性:

 [DirectoryRdnPrefix("CN")] [DirectoryObjectClass("Person")] public class UserPrincipalEx : UserPrincipal { // Inplement the constructor using the base class constructor. public UserPrincipalEx(PrincipalContext context) : base(context) { } // Implement the constructor with initialization parameters. public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled) : base(context, samAccountName, password, enabled) {} // Create the "Department" property. [DirectoryProperty("department")] public string Department { get { if (ExtensionGet("department").Length != 1) return string.Empty; return (string)ExtensionGet("department")[0]; } set { ExtensionSet("department", value); } } // Create the "Manager" property. [DirectoryProperty("manager")] public string Manager { get { if (ExtensionGet("manager").Length != 1) return string.Empty; return (string)ExtensionGet("manager")[0]; } set { ExtensionSet("manager", value); } } // Implement the overloaded search method FindByIdentity. public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue) { return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue); } // Implement the overloaded search method FindByIdentity. public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue) { return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue); } } 

现在,您可以在代码中使用UserPrincipalEx的“扩展”版本:

 using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) { // Search the directory for the new object. UserPrincipalEx inetPerson = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser"); // you can easily access the Manager or Department now string department = inetPerson.Department; string manager = inetPerson.Manager; }