2014-05-20 37 views
0

我有autoComplate文本框。我需要用AD使用Ajax填充samAccountName,并在AD中搜索时使用LIKE。如何使用LIKE在Active Directory中搜索?

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

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
searcher = new DirectorySearcher(string.Format("(&(samAccountType=805306368)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(samAccountName=*{0}*))", username)); 

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

相关问题