简体   繁体   English

如何通过诸如Department之类的属性从Active Directory获取用户列表

[英]How to get list of Users from Active Directory by attributes such as Department

I need to provide a search on Active Directory using filters such as Display name, Telephone and Department. 我需要使用显示名称,电话和部门等过滤器在Active Directory上进行搜索。 Display name and Telephone are easy but I'm stuck on department. 显示名称和电话很简单,但我受困于部门。 This is the bit that works: 这是可行的:

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

I was hoping that I could just add something like: 我希望我可以添加如下内容:

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

But that doesn't work. 但这是行不通的。 Anyone know how I can do this? 有人知道我该怎么做吗?

Edit: I'm an idiot, changed the search term and found the answer. 编辑:我是个白痴,更改了搜索词并找到了答案。 The extra fields are called attibutes. 额外的字段称为attibutes。 Thanks Raymund Macaalay for your blog article on extending Principals . 感谢Raymund Macaalay关于扩展Principals的博客文章

My extended UserPrincipal: 我扩展的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); }
    }
} 

Since you've already extended the UserPrincipal to include the Department attribute, you'll need to use that extended version of the user principal when you want to search. 由于已经将UserPrincipal扩展为包括Department属性,因此在搜索时需要使用用户主体的扩展版本。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何从Active Directory中的每个部门获取组列表 - C# How to get a list of groups from each department in Active Directory 给定一个UserPrincipal对象,如何从Active Directory中获取“公司”和“部门”? - How to get “Company” and “Department” from Active Directory given a UserPrincipal object? 如何从活动目录中获取用户列表? - How can I get a list of users from active directory? 如何从Active Directory中获取用户头像? - How to get users avatar from Active Directory? 我可以使用PrincipalContext()从Active Directory获取用户部门吗 - Can I get the User Department from Active Directory using PrincipalContext() 从给定AD组中的Active Directory获取用户列表 - Get List of Users From Active Directory In A Given AD Group 如何从Active Directory获取组中每个成员的属性列表,如名字,姓氏,电子邮件 - How to get list of Attributes for each member of a group from Active Directory such as First Name, Last Name, Email Active Directory:如何根据特定的组织单位获取用户列表 - Active Directory: How to get a list of users according to a specific Organization unit .NET如何在Active Directory中搜索和获取用户列表 - .NET How to search and get list of users in Active Directory 如何使用LINQ to LDAP获取活动目录中的用户列表? - how can get List of users in active directory using LINQ to LDAP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM