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