title: How I can use the Active Directory for AutoComplete in a Webservice
body: <p>I want built a ASP.NET Application with a AutoComplete with Ajax. I want use the Active Directory to get Data how Displayname. </p>
  
  <p>Here my Code:</p>
  
  <p>ASPX:</p>
  
  <pre><code>    &lt;tr&gt;
              &lt;td&gt;&lt;asp:Label ID="lblVertreter" runat="server" /&gt;&lt;/td&gt;
              &lt;td&gt;
              &lt;asp:TextBox ID="txtVertreter" runat="server"&gt;&lt;/asp:TextBox&gt;
              &lt;asp:AutoCompleteExtender ID="AutoComlete" runat="server" TargetControlID="txtVertreter" ServiceMethod="MitarbeiterErmitelln" ServicePath="Service1.asmx"&gt;&lt;/asp:AutoCompleteExtender&gt;
              AutoCompleteExtender
              &lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>asmx code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Services;
  using System.Data;
  using System.DirectoryServices;
  
  namespace ADWebService
  {
  
      [WebService(Namespace = "http://tempuri.org/")]
      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
      [System.ComponentModel.ToolboxItem(false)]
  
      public class Service1 : System.Web.Services.WebService
      {
          public static string GetLdapProperty(SearchResult searchResult, string PropertyName)
          {
  
              if (searchResult.Properties.Contains(PropertyName))
              {
                  return searchResult.Properties[PropertyName][0].ToString();
              }
              else
              {
                  return string.Empty;
              }
  
          }
  
          [WebMethod]
          public string[] MitarbeiterErmitelln(string prefix)
          {
              string filterung = "((&amp;(objectClass=user)(objectCategory=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(sn=*)(|(telephoneNumber=*)(mail=*))(displayName=*" + prefix + "*)))";
  
              string ADPfad = "Domain";
  
              int Counter = 0;
  
              List&lt;Benutzer&gt; result = new List&lt;Benutzer&gt;();
  
              DirectoryEntry Entry = new DirectoryEntry(ADPfad);
  
              DirectorySearcher Searcher = new DirectorySearcher(Entry, filterung);
  
              foreach (SearchResult usr in Searcher.FindAll())
              {
                  result.Add(new Benutzer()
                  {
                      DisplayName = GetLdapProperty(usr, "displayName"),
  
                  });
                  Counter++;
              }
  
              return result.ToArray(); 
          }
  
      }
  }
  
  //The Class Benutzer is my Model Class
  </code></pre>
  
  <p>What is wrong. I want use direct the Active Directory ! </p>