我有自动完成文本框。我需要使用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());}
答案 0 :(得分:1)
searcher = new DirectorySearcher(string.Format("(&(samAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(samAccountName=*{0}*))", username));
此MSDN页面提供了搜索语法的简短概述。