2012-09-11 33 views
8

Tôi cần cung cấp tìm kiếm trên Active Directory bằng các bộ lọc như Tên hiển thị, Điện thoại và Bộ. Tên hiển thị và điện thoại thật dễ dàng nhưng tôi bị kẹt trên bộ phận. Đây là bit hoạt động:Cách lấy danh sách Người dùng từ Active Directory theo các thuộc tính như Bộ

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    UserPrincipal userPrincipal = new UserPrincipal(context); 

    if (txtDisplayName.Text != "") 
     userPrincipal.DisplayName = "*" + txtDisplayName.Text + "*"; 

    using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal)) 
    { 
     foreach (Principal result in searcher.FindAll()) 
     { 
      DirectoryEntry directoryEntry = result.GetUnderlyingObject() as DirectoryEntry; 
      DataRow drName = dtProfile.NewRow(); 
      drName["displayName"] = directoryEntry.Properties["displayName"].Value; 
      drName["department"] = directoryEntry.Properties["department"].Value; 
      dtProfile.Rows.Add(drName); 
     } 
    } 
} 

Tôi đã hy vọng rằng tôi có thể chỉ cần thêm một cái gì đó như:

DirectoryEntry userDirectoryEntry = userPrincipal.GetUnderlyingObject() as DirectoryEntry; 
if (ddlDepartment.SelectedValue != "") 
    userDirectoryEntry.Properties["title"].Value = ddlDepartment.SelectedValue; 

Nhưng điều đó không làm việc. Bất cứ ai biết làm thế nào tôi có thể làm điều này?

Chỉnh sửa: Tôi là kẻ ngốc, đã thay đổi cụm từ tìm kiếm và tìm thấy câu trả lời. Các trường bổ sung được gọi là attibutes. Thanks Raymund Macaalay for your blog article on extending Principals.

UserPrincipal mở rộng của tôi:

[DirectoryObjectClass("user")] 
[DirectoryRdnPrefix("CN")] 

public class UserPrincipalExtended : UserPrincipal 
{  
    public UserPrincipalExtended(PrincipalContext context) : base(context) 
    { 
    } 
    [DirectoryProperty("department")] 
    public string department 
    { 
     get 
     { 
      if (ExtensionGet("department").Length != 1) 
       return null; 
      return (string)ExtensionGet("department")[0]; 
     } 
     set { this.ExtensionSet("department", value); } 
    } 
} 

Trả lời

5

Vì bạn đã mở rộng UserPrincipal để bao gồm các thuộc tính Department, bạn sẽ cần phải sử dụng mở rộng phiên bản của chính người sử dụng khi bạn muốn tìm kiếm.

Hãy thử cách này:

using (PrincipalContext context = new PrincipalContext(ContextType.Domain)) 
{ 
    UserPrincipalExtended userPrincipal = new UserPrincipalExtended(context); 

    if (txtDisplayName.Text != "") 
    { 
     userPrincipal.DisplayName = "*" + txtDisplayName.Text + "*"; 
    } 

    if (!string.IsNullOrEmpty(txtDepartment.Text.Trim()) 
    { 
     userPrincipal.department = txtDepartment.Text.Trim(); 
    } 

    using (PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal)) 
    { 
     foreach (Principal result in searcher.FindAll()) 
     { 
      UserPrincipalExtended upe = result as UserPrincipalExtended; 

      if (upe != null) 
      { 
       DataRow drName = dtProfile.NewRow(); 
       drName["displayName"] = upe.DisplayName; 
       drName["department"] = upe.department; 
       dtProfile.Rows.Add(drName); 
      } 
     } 
    } 
} 
Các vấn đề liên quan