如何使用 VB.NET 查询 LDAP 并显示您域的全局地址列表





0/5 (0投票)
快速浏览如何快速查询 LDAP 以及如何使用该查询的结果。
引言
不久前,我想快速查询 LDAP 以创建一个简单的非域用户的域全局地址列表渲染。 与标准的 T-SQL 和 Oracle 数据源不同, 我发现没有太多信息显示查询数据的简单方法,而且一旦我得到它, 我需要一种简单的方法来显示它,并且将其绑定到传统的 Gridview 似乎不合时宜,考虑到此数据的非常规来源。 我希望通过这篇文章为您提供一个简单的页面加载事件,您可以将其放入一个空白的 asp 模板中并立即看到结果。 当然,这不仅仅是一个代码示例, 我想解释每一部分的作用,以便您也可以根据您的需要进行自定义。开始之前
本文假定您通过 IIS 从作为域成员的 PC/服务器运行 ASP 内容。 我们不会指定凭据或 FQDN,因此这些值将使用您当前的凭据和系统域成员身份进行检索。 另外,在您的代码后台,当您导入“System.DirectoryServices”时,如果您遇到问题,您可能需要添加对其的网络引用,这很容易,因为它应该在您从项目中选择添加引用时列在“Net”选项卡上。让我们开始吧
- 向您的 HTML 表添加一个简单的标题行。 这是通过
Response.Write
命令完成的,就像我们将要创建的表的其余部分一样。 因此,在您的代码背后的Page_Load
事件中,粘贴以下行以编写基本的 HTML 表头行 - 接下来声明一个变量,该变量将用于引用
System.DirectoryServices
命名空间中的DirectorySearcher
类属性。 - 接下来声明一个字符串并用
DirectorySearcher
类中找到的SearchRoot
Path 属性填充它。 - 现在,使用我们之前创建的
objsearch
变量,我们需要在启动 AD 搜索之前设置一些属性和值。 此块将搜索过滤到用户对象,定义搜索范围,并 指定搜索中“通用名称”对象属性的返回。 - 接下来,使用此块,我们指定我们只想检索具有已分配值的属性的名称,以及 定义排序属性和方向。
- 现在我们定义一个
SearchResultCollection
类型的集合变量并调用DirectorySearcher
类的FindAll
方法。 当然,这会执行查询并将结果放入此变量中。 - 最后,使用 For Each 循环,我们将 遍历集合中的项目,并使用
Response.Write
方法 将每一行的内容写入我们的 HTML 表中。 - 最后,我们通过编写 HTML 表的结束标记来结束该过程。
Response.Write("<table border=1 bordercolor=#000000><tr> " & _
"<td bgcolor=#ffff99 width=190px align=center>Employee Name</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Phone</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Mobile</td>" & _
"<td bgcolor=#ffff99 width=250px align=center>E-mail Address</td></tr>")
Dim objsearch As DirectorySearcher = New DirectorySearcher
Dim strrootdse As String = objsearch.SearchRoot.Path
有关 AD 对象 属性的更多信息,请参阅底部的对象引用链接
objsearch.Filter = "(& (mailnickname=*)(objectClass=user))"
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
objsearch.PropertiesToLoad.Add("cn")
objsearch.PropertyNamesOnly = True
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
objsearch.Sort.PropertyName = "cn"
Dim colresults As SearchResultCollection = objsearch.FindAll
For Each objresult As SearchResult In colresults
Response.Write("<tr><td>" & objresult.GetDirectoryEntry.Properties("cn").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("telephoneNumber").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("mobile").Value & _
"</td><td><a href='mailto:" & objresult.GetDirectoryEntry.Properties("mail").Value & "'>" & _
objresult.GetDirectoryEntry.Properties("mail").Value & "</a></td></tr>")
Next
Response.Write("</table>")
因为这一切都在页面加载事件中,只需在 IIS 中访问页面或在 VS/VWD 中开始调试,您应该会看到一个快速简便的 AD 属性列表。
最终结果
我希望您能看到如何快速将其合并到您自己的项目中,无论它是否涉及验证域帐户或显示域成员数据。 这是已完成的页面加载事件。
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Response.Write("<table border=1 bordercolor=#000000><tr> " & _
"<td bgcolor=#ffff99 width=190px align=center>Employee Name</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Phone</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Mobile</td>" & _
"<td bgcolor=#ffff99 width=250px align=center>E-mail Address</td></tr>")
Dim objsearch As DirectorySearcher = New DirectorySearcher
Dim strrootdse As String = objsearch.SearchRoot.Path
objsearch.Filter = "(& (mailnickname=*)(objectClass=user))"
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
objsearch.PropertiesToLoad.Add("cn")
objsearch.PropertyNamesOnly = True
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
objsearch.Sort.PropertyName = "cn"
Dim colresults As SearchResultCollection = objsearch.FindAll
For Each objresult As SearchResult In colresults
Response.Write("<tr><td>" & objresult.GetDirectoryEntry.Properties("cn").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("telephoneNumber").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("mobile").Value & _
"</td><td><a href='mailto:" & _
objresult.GetDirectoryEntry.Properties("mail").Value & "'>" & _
objresult.GetDirectoryEntry.Properties("mail").Value & _
"</a></td></tr>")
Next
Response.Write("</table>")
End Sub