Get List of all users with the same physicalDeliveryOfficeName from AD in Visual C#

时间:2016-07-11 19:39:39

标签: c# asp.net ldap directorysearcher

I'd like to search my active directory for all users that belong to a particular physicalDeliveryOfficeName (LDAP) and store them into an array of type SearchResult. Can I do this with a DirectorySearcher filter? Or is there a better approach?

I'm using asp.net, visual c#. Thanks!

1 个答案:

答案 0 :(得分:0)

使用DirectorySeacher类,您的查询可能是

(&(objectClass=user)(physicalDeliveryOfficeName=Kalkutta))

其中 objectClass = user 仅获取用户条目,而 physicalDeliveryOfficeName = Kalkutta 是您对办公室的查询。

DirectoryEntry entry = new DirectoryEntry("LDAP://...");           
DirectorySearcher search = new DirectorySearcher(entry)
    {
         SearchScope = SearchScope.Subtree,
         Filter = "(&(objectClass=user)(physicalDeliveryOfficeName=Kalkutta))"
    };
search.PropertiesToLoad.Add("cn");
SearchResultCollection result = search.FindAll();
foreach (SearchResult r in result)
     Response.Write(r.Properties["cn"][0]);