2013-03-25 33 views
6

Bắt đầu sử dụng không gian tên System.DirectoryServices.AccountManagement, để thực hiện tra cứu người dùng trong thư mục hoạt động (AD). Tôi cũng cần người quản lý của người dùng, nhưng dường như tôi đã gặp phải một cú hích trên đường bằng không gian tên này. mã hiện tại để có được một người:C# - Tra cứu người quản lý người dùng trong thư mục hoạt động

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(); 
     } 
    } 

Execute tìm kiếm cho người

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

     return user; 
    } 

một cách khác để truy cập trực tiếp các tên phân biệt của người quản lý của một người dùng cụ thể là gì?

  • Câu trả lời một phần có thể here trong VB, nhưng tôi không thấy gì liên quan đến người quản lý.
  • Một phần khác có thể là here, một lần nữa, không có gì về người quản lý.

Trả lời

7

Nếu bạn đang ở trên .NET 3.5 và lập và sử dụng các System.DirectoryServices.AccountManagement (S.DS.AM) namespace, bạn có thể dễ dàng mở rộng các lớp UserPrincipal hiện có để có được thuộc tính nâng cao hơn, như Manager, vv

đọc tất cả về nó ở đây:

.210

Về cơ bản, bạn chỉ cần xác định một lớp được thừa kế dựa trên UserPrincipal, và sau đó bạn xác định tính chất bổ sung của bạn mà bạn muốn:

[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); 
    } 
} 

Bây giờ, bạn có thể sử dụng các "đại gia" phiên bản của UserPrincipalEx trong mã của bạn:

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

Bạn đã thử điều này chưa? Nó không hoạt động, đối với tôi. UserPrincipalEx.FindByIdentity không trả về một đối tượng UserPrincipalEx, và truyền tới UserPrincipalEx gây ra một InvalidCastException. – Naikrovek

+1

@Naikrovek: xin lỗi - lỗi của tôi - tôi đã cắt ra một chút quá nhiều mã từ mẫu (còn lâu hơn) của tôi. Tôi đã bỏ lỡ hai phương thức 'FindByIdentity' và' FindByIdentityWithType' bị quá tải - tôi đã thêm vào đoạn mã của mình - và có, với mã này, tôi vừa kiểm tra nó với Win Server 2008 R2 Active Directory và nó hoạt động tốt cho tôi. –

+0

Hoạt động tuyệt vời, cảm ơn bạn. – Naikrovek

Các vấn đề liên quan