0

我想用 Ajax 构建一个带有自动完成功能的 ASP.NET 应用程序。我想使用 Active Directory 来获取数据如何显示名称。

这是我的代码:

ASPX:

    <tr>
            <td><asp:Label ID="lblVertreter" runat="server" /></td>
            <td>
            <asp:TextBox ID="txtVertreter" runat="server"></asp:TextBox>
            <asp:AutoCompleteExtender ID="AutoComlete" runat="server" TargetControlID="txtVertreter" ServiceMethod="MitarbeiterErmitelln" ServicePath="Service1.asmx"></asp:AutoCompleteExtender>
            AutoCompleteExtender
            </td>
</tr>

asmx代码:

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 = "((&(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<Benutzer> result = new List<Benutzer>();

            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

怎么了。我想直接使用 Active Directory !

4

0 回答 0