如何使用LIKE在Active Directory中搜索?

时间:2014-05-20 10:00:18

标签: ajax search active-directory jquery-autocomplete

我有自动完成文本框。我需要使用Ajax使用来自AD的samAccountName填充,并在AD中搜索时使用LIKE。

这是我的代码,但我有错误:

 List<string> result = new List<string>();
    DirectoryEntry domain1 = default(DirectoryEntry);
    DirectorySearcher searcher = default(DirectorySearcher);
    domain1 = new DirectoryEntry();
    searcher = new DirectorySearcher("(&(samAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(samAccountName LIKE N'%" + username + "%'))");
    searcher.SearchRoot = domain1;
    searcher.SearchScope = SearchScope.Subtree;
    DataTable dtfill = new DataTable();
    dtfill.Columns.Add("UserName");

    using (System.DirectoryServices.SearchResultCollection userlist = searcher.FindAll())
    {
        for (int i = 0; i <= userlist.Count - 1; i++)
result.Add(Convert.ToString(userlist[i].GetDirectoryEntry().Properties["samAccountName"].Value).ToLower());}

1 个答案:

答案 0 :(得分:1)

searcher = new DirectorySearcher(string.Format("(&(samAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(samAccountName=*{0}*))", username));

MSDN页面提供了搜索语法的简短概述。