by IISMATT
15. June 2009 09:39
I have had to grab the users from Active Directory and bind them to an ASP.NET Control in order to display them in web page, and after doing some testing and fishing around, I came across a method that worked for me after some adjustments and trial and error...
'vb.net CodeBehind===============================
Imports System.DirectoryServices
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnBindUsers_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBindUsers.Click
Dim ADPath As String = "LDAP://OU=Users,DC=yourdmain,DC=local"
Dim dEntry As New DirectoryEntry(ADPath)
Dim dSearcher As New DirectorySearcher(dEntry)
' You do not want to make this too large if you have a big directory
dSearcher.PageSize = 250
dSearcher.Filter = "(objectCategory=Person)"
'Create array to sort records alphabetically
Dim theUsers As ArrayList
theUsers = New ArrayList
Dim theResults As SearchResult
For Each theResults In dSearcher.FindAll()
theUsers.Add(theResults.Properties("cn")(0))
Next theResults
theUsers.Sort()
'demo of a list box
lstUser.DataSource = theUsers
lstUser.DataBind()
'demo of a DropDownList
ddlUsers.DataSource = theUsers
ddlUsers.DataBind()
dEntry.Dispose()
End Sub
End Class
'End vb.net CodeBehind===============================
Asp.Net HTML Page===================================
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="DebtTracker._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnBindUsers" runat="server" Text="Show Users" />
<br />
<asp:ListBox ID="lstUser" runat="server" Height="175px" Width="150px" />
<br />
<asp:DropDownList ID="ddlUsers" runat="server" Height="20px" Width="150px" />
</div>
</form>
</body>
</html>
End Asp.Net HTML Page===================================


