Active Directory Web Service and AutoCompleteExten

2019-08-30 04:29发布

问题:

This is my first time playing with Active Directory, as well as the Ajax Control Toolkit. What I'm trying to do is when a user starts to type their name in a textbox, it will attempt to auto-complete their name as listed in AD. Every example I can find on using the AutoCompleteExtender, it's referring to an ASMX file in the ServicePath.

I've managed to figure out how to add our AD web service as a service reference to my project. Can anyone provide any guidance or examples on how I can get AutoCompleteExtender to recognize and play with my AD service reference? Is it even possible?

Thank you for any assistance.

回答1:

After much research and help from our system admins to get the correct LDAP path, I've finally got this working. I'm posting the code so other's can benefit from it. (Note that I first installed AJAX Control Toolkit for .NET 4.0.)

Default.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();
        }
    }
}

Enjoy!