【问题标题】:How to get list of Users from Active Directory by attributes such as Department如何通过部门等属性从 Active Directory 中获取用户列表
【发布时间】:2012-09-11 00:11:35
【问题描述】:

我需要使用显示名称、电话和部门等过滤器在 Active Directory 上提供搜索。显示名称和电话很容易,但我被困在部门上。这是有效的一点:

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

我希望我可以添加如下内容:

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

但这不起作用。有人知道我该怎么做吗?

编辑: 我是个白痴,更改了搜索词并找到了答案。额外的字段称为属性。 Thanks Raymund Macaalay for your blog article on extending Principals.

我的扩展 UserPrincipal:

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

【问题讨论】:

    标签: c# asp.net active-directory


    【解决方案1】:

    由于您已经扩展了UserPrincipal 以包含Department 属性,因此当您想要搜索时,您需要使用该扩展 版本的用户主体。

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多