经过我们系统管理员的大量研究和帮助以获取正确的 LDAP 路径,我终于得到了这个工作。我正在发布代码,以便其他人可以从中受益。(请注意,我首先为 .NET 4.0 安装了 AJAX 控制工具包。)
默认.aspx:
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True"></ajaxToolkit:ToolkitScriptManager>
<asp:TextBox ID="txtSearchAD" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ServiceMethod="findEmp"
MinimumPrefixLength="3"
CompletionInterval="100"
EnableCaching="true"
CompletionSetCount="10"
TargetControlID="txtSearchAD"
ID="ace1"
runat="server"
FirstRowSelected="false" ServicePath="ADS.asmx">
</ajaxToolkit:AutoCompleteExtender>
ADS.asmx.cs:
namespace EventTracking
{
/// <summary>
/// Summary description for ADS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class ADS : System.Web.Services.WebService
{
[WebMethod]
public string[] findEmp(string prefixText, int count)
{
DirectoryEntry directory = new DirectoryEntry();
directory.Path = "LDAP://DC=yourdomain,DC=com"; //CHANGE to your LDAP path
string filter = "(&(cn=" + prefixText + "*))";
string[] strCats = { "cn" };
List<string> items = new List<string>();
DirectorySearcher dirComp = new DirectorySearcher(directory, filter, strCats, SearchScope.Subtree);
SearchResultCollection results = dirComp.FindAll();
foreach (SearchResult result in results)
{
foreach (DictionaryEntry prop in result.Properties)
{
if (prop.Key.Equals("cn"))
{
System.Collections.IEnumerable propsEnum = prop.Value as System.Collections.IEnumerable;
foreach (object individualValue in propsEnum)
{
if (individualValue.ToString().IndexOf(prefixText) != 0)
{
items.Add(individualValue.ToString());
}
}
}
}
}
return items.ToArray();
}
}
}
享受!