Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有Active Directory用户的自动完成文本框_C#_Json_Asp.net Mvc_Active Directory - Fatal编程技术网

C# 具有Active Directory用户的自动完成文本框

C# 具有Active Directory用户的自动完成文本框,c#,json,asp.net-mvc,active-directory,C#,Json,Asp.net Mvc,Active Directory,您好,我正在尝试创建一个文本框,当用户在其中键入时,他们会得到一个具有特定名称的用户列表: 示例:如果我开始键入Jane.Doe,而我只键入了Ja,则会出现一个列表,列出以Ja开头的Active Directory用户。我需要弄清楚如何在每次用户输入时让用户进入列表。我几乎完成了ajax方面的工作。它只是每次更新用户列表 我目前的想法是: [HttpPost] public ActionResult RemoteData(string query) { List

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

示例:如果我开始键入Jane.Doe,而我只键入了Ja,则会出现一个列表,列出以Ja开头的Active Directory用户。我需要弄清楚如何在每次用户输入时让用户进入列表。我几乎完成了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 });
    }
[HttpPost]
公共操作结果远程数据(字符串查询)
{
List lstADUsers=新列表();
使用(var context=new PrincipalContext(ContextType.Domain,null,“LDAPPATH”))
{
使用(var searcher=newprincipalsearcher(newuserprincipal(context)))
{
foreach(searcher.FindAll()中的var结果)
{
DirectoryEntry de=result.getUnderlineObject()作为DirectoryEntry;
字符串usersWithName;
如果(!String.IsNullOrEmpty((String)de.Properties[“samaccountname”].Value))
{
usersWithName=de.Properties[“samaccountname”].Value.ToString();
lstADUsers.Add(usersWithName);
}
}
}
}
List listData=null;
如果(!string.IsNullOrEmpty(查询))
{
listData=lstADUsers.Where(q=>q.ToLower().StartsWith(query.ToLower()).ToList();
}   
返回Json(new{Data=listData});
}

因此,这允许我们获取Active Directory中的每个用户,但我不希望这样做,因为当前的问题是,用户太多,搜索需要花费很长时间才能加载,甚至在它显示名称列表之前。我只希望能够获取一个参数,并且只搜索以该参数开头的用户。如何执行此操作?

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

// 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嗨,我是广告查询新手。。我有个问题。在循环searcher.findall中,结果本身包含属性名称中的用户名。那么为什么它使用DirectoryEntry对象呢?