2013-03-25 25 views
6

开始使用System.DirectoryServices.AccountManagement命名空间,对活动目录(AD)中的用户执行查找。 我也需要用户的经理,但我似乎在使用这个命名空间的道路上碰到了一个碰撞。当前的代码来获得一个人:C# - 在活动目录中查找用户管理器

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.<NO SUCH PROPERTY TO FIND A MANAGER'S DISTINGUISHED NAME> 
      } 
      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; 
    } 

什么是另一种方式来直接访问特定用户的管理者的专有名称?

  • 可能部分答案here在VB,但我看不到任何关于参照管理人员。
  • 另一个可能的部分here,再次,没有关于经理。

回答

7

如果你在.NET 3.5和和使用System.DirectoryServices.AccountManagement(S.DS.AM)命名空间,你可以很容易地扩展现有UserPrincipal类来获得更高级的属性,如Manager

阅读所有关于它在这里:

基本上,你只是定义基于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; 
}   
+0

你试过吗?这对我来说不起作用。 UserPrincipalEx.FindByIdentity不返回UserPrincipalEx对象,并且转换为UserPrincipalEx会导致InvalidCastException。 – Naikrovek 2013-03-27 19:47:45

+1

@Naikrovek:对不起 - 我的错误 - 我从我的(更长的)样本中删除了太多的代码。我错过了两个重载的'FindByIdentity'和'FindByIdentityWithType'方法 - 我将这些添加到了我的代码片段 - 是的,通过这段代码,我只是对照Win Server 2008 R2 Active Directory进行了检查,并且对我来说工作正常。 – 2013-03-27 22:10:48

+0

很好,谢谢。 – Naikrovek 2013-03-28 13:08:33

相关问题