【问题标题】:Autocomplete Text Box With Active Directory Users具有 Active Directory 用户的自动完成文本框
【发布时间】:2016-01-28 15:36:52
【问题描述】:

您好,我正在尝试创建一个文本框,当用户在其中输入时,他们会获得具有特定名称的用户列表:

示例:如果我开始键入 Jane.Doe,而我只键入了 Ja,那么会从 Active Directory 中列出以 Ja 开头的用户。我需要弄清楚每次用户键入时如何将用户添加到列表中。我几乎完成了ajax方面的工作。它只是每次更新用户列表。

我目前的想法:

 [HttpPost]
    public ActionResult RemoteData(string query)
    {
        List<string> lstADUsers = new List<string>();

        using (var context = new PrincipalContext(ContextType.Domain, null, "LDAPPATH"))
        {
            using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
            {
                foreach (var result in searcher.FindAll())
                {
                    DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;

                    string usersWithName;


                    if (!String.IsNullOrEmpty((String)de.Properties["samaccountname"].Value))
                    {
                        usersWithName = de.Properties["samaccountname"].Value.ToString();



                        lstADUsers.Add(usersWithName);
                    }

                }
            }
        }

        List<string> listData = null;
        if (!string.IsNullOrEmpty(query))
        {

            listData = lstADUsers.Where(q => q.ToLower().StartsWith(query.ToLower())).ToList();

        }   

        return Json(new { Data = listData });
    }

因此,这允许我们获取 Active Directory 中的每个用户,但我不希望这样做,因为手头的问题是用户太多,并且搜索需要 FOREVER 才能加载它甚至显示名称列表.我只希望能够获取一个参数并且只搜索以该参数开头的用户。我该怎么做呢?

【问题讨论】:

    标签: c# json asp.net-mvc active-directory


    【解决方案1】:

    您需要使用通配符填充UserPrincipalName 属性以限制结果集:

    // assume 'query' is 'Ja'
    UserPrincipal user = new UserPrincipal(context);
    user.Name = query + "*"; // builds 'Ja*', which finds names starting with 'Ja'
    using (var searcher = new PrincipalSearcher(user))
    // ...
    

    【讨论】:

    • 我收到此错误:“System.DirectoryServices.AccountManagement.UserPrincipal.UserPrincipal(System.DirectoryServices.AccountManagement.PrincipalContext)”的最佳重载方法匹配有一些无效参数
    • 我的错误,意外地实例化了 UserPrincipal 两次。更新了代码。再试一次。
    • @Chase 嗨,我是 AD 查询的新手。我有疑问。在循环 searcher.findall 中,结果本身包含属性名称中的用户名。那么为什么它使用 DirectoryEntry 对象呢?
    猜你喜欢
    • 2010-11-14
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2010-09-22
    • 1970-01-01
    相关资源
    最近更新 更多