Imports SQLMonitor.SQLFunctions Imports SQLMonitor.SQLMonLib Imports System.Text Imports System.DirectoryServices Imports System.Data Imports System.Data.SqlClient Partial Class Utilities_AD_Users Inherits System.Web.UI.Page Dim gPwd As String Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not IsPostBack Then txtDomainController.Text = System.Configuration.ConfigurationManager.AppSettings.Item("DomainController") txtUserId.Text = User.Identity.Name Else gPwd = txtPassword.Text End If End Sub Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click Dim strDNSDomain As String = "" Dim strDomainController As String = "" Dim strDirectoryEntry As String = "" Dim objDirEnt As DirectoryServices.DirectoryEntry If chkImpersonate.Checked = True Then If txtUserId.Text.Length = 0 Or txtPassword.Text.Length = 0 Then lblMessage.Text = "A User Id and Password must be specified, it will not be saved, but is used to validate against active directory." lblMessage.Visible = True Exit Sub End If End If Try strDomainController = "LDAP://" & txtDomainController.Text strDirectoryEntry = strDomainController + "/rootDSE" 'Dim objDirEnt As New DirectoryServices.DirectoryEntry(strDirectoryEntry, txtUserId.Text, gPwd, AuthenticationTypes.Secure) If chkImpersonate.Checked = False Then objDirEnt = New DirectoryServices.DirectoryEntry(strDirectoryEntry) Else objDirEnt = New DirectoryServices.DirectoryEntry(strDirectoryEntry, txtUserId.Text, gPwd, AuthenticationTypes.Secure) End If strDNSDomain = objDirEnt.Invoke("Get", "defaultNamingContext").ToString() Dim dt As New DataTable dt = GetAllUsers(strDomainController, strDNSDomain) lblMessage.Text = dt.Rows.Count.ToString() & " accounts found." lblMessage.Visible = True grdUsers.DataSource = dt.DefaultView() grdUsers.DataBind() grdUsers.Visible = True Catch ex As Exception lblMessage.Text = "An Error Occurred: " & ex.Message lblMessage.Visible = True End Try End Sub Private Function GetAllUsers(ByVal strDomain As String, _ ByVal strDSN As String) As DataTable Dim strDirectory As String = "" '"LDAP://myserver.mydomain.com/DC=mydomain,DC=com" Dim adEntry As DirectoryEntry strDirectory = strDomain & "/" & strDSN ' setting up the lookup to AD ' Dim adEntry As New DirectoryEntry(strDirectory, txtUserId.Text, txtPassword.Text, AuthenticationTypes.Secure) If chkImpersonate.Checked = False Then adEntry = New DirectoryEntry(strDirectory) 'adEntry.AuthenticationType = AuthenticationTypes.Secure Else adEntry = New DirectoryEntry(strDirectory, txtUserId.Text, txtPassword.Text, AuthenticationTypes.Secure) End If ' define which fields to retrieve from AD Dim adSearcher As New DirectorySearcher(adEntry) Dim strSearchString As String = "" If cboSearchType.SelectedIndex = 0 Then strSearchString = "(samaccountname=" & txtSearchString.Text & ")" Else ' strSearchString = "(sn=Hor*)" strSearchString = "(sn=" & txtSearchString.Text & "*)" End If adSearcher.Filter = "(&(objectCategory=person)(objectClass=user)" & strSearchString & ")" adSearcher.PropertiesToLoad.Add("cn") adSearcher.PropertiesToLoad.Add("sAMAccountName") adSearcher.PropertiesToLoad.Add("mail") ' define a datatable and add the results to it Dim adResults As SearchResultCollection Dim dt As New DataTable("AD_Users") dt.Columns.Add(New DataColumn("AccName", GetType(System.String))) dt.Columns.Add(New DataColumn("Name", GetType(System.String))) dt.Columns.Add(New DataColumn("Email", GetType(System.String))) Dim dr As DataRow adResults = adSearcher.FindAll For Each adResult As SearchResult In adResults ' add the results to the datatable dr = dt.NewRow() Try dr(0) = adResult.Properties("sAMAccountName")(0).ToString() dr(1) = adResult.Properties("cn")(0).ToString() dr(2) = adResult.Properties("mail")(0).ToString() Catch ex As Exception End Try dt.Rows.Add(dr) Next Return dt End Function End Class