小编And*_*itt的帖子

如何通过Department等属性从Active Directory获取用户列表

我需要使用显示名称,电话和部门等过滤器在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);
        }
    }
} 
Run Code Online (Sandbox Code Playgroud)

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

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

但这不起作用.谁知道我怎么做到这一点?

编辑:我是个白痴,改变了搜索词,找到了答案.额外的字段称为attibutes.感谢Raymund Macaalay关于扩展校长的博客文章.

我扩展的UserPrincipal:

[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")] …
Run Code Online (Sandbox Code Playgroud)

c# asp.net active-directory

8
推荐指数
1
解决办法
6205
查看次数

标签 统计

active-directory ×1

asp.net ×1

c# ×1