2014-05-20 36 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頁面提供了語法搜索一個簡短的概述。

相關問題