Skip to sidebar Skip to content
Ipercube Technical
Jack of all trades, master of none
Date: Time: - Stardate:
Welcome, Log in by clicking  Here!
SS64
MDN
Stack
Code
GitHub
Wiki
Convert
V Studio
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions
Ipercube Technical
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions

Query Users

Executes an LDAP search query to retrieve user information from Active Directory.
  • Uses an DirectoryServices.Protocols.LdapConnection and DirectoryServices.Protocols.SearchRequest to perform the search query.
  • Authentication is done using the provided username, pwd, and domain.
  • If propertyList is empty, all available LDAP attributes are retrieved.
VB
    Public Function ldQueryUsers(QueryString As String, 
                                 server As String, 
                                 domain As String, 
                                 username As String, 
                                 pwd As String, 
                                 Optional propertyList As String = "") 
                               As (SearchResponse As SearchResponse, ErrMessage As String)
                                 
        Dim ErrMessage As String = String.Empty
        Dim propArray As String() = {}

        'Ldap prop array
        If propertyList.Length > 0 Then
            propArray = Split(propertyList, ",")
        End If

        'Setup ldap server and credential
        Dim ldapCon As LdapConnection = New LdapConnection(server)
        Dim networkCreds As NetworkCredential = New NetworkCredential(username, pwd, domain)

        'configure the connection and bind
        ldapCon.AuthType = AuthType.Negotiate
        Try
            ldapCon.Bind(networkCreds)
        Catch ex As Exception
            ErrMessage = "Error connecting to LDAP server:  " + server + vbCrLf + ex.Message
            Return (Nothing, ErrMessage)
        End Try

        'Issue the search request - If Property array is empty, all properties are retrieved
        Dim searchRequest = New SearchRequest(domain, QueryString, DirectoryServices.SearchScope.Subtree, propArray)

        'Issue the search request, and check the results
        Try
            Dim searchResult As SearchResponse = ldapCon.SendRequest(searchRequest)
            If searchResult.Entries.Count > 0 Then Return (searchResult, String.Empty)
        Catch ex As Exception
            ErrMessage += "Error querying LDAP:" + vbCrLf + 
                          ex.Message + vbCrLf + 
                          "Server: " + server + vbCrLf + 
                          "Query:" + vbCrLf + QueryString + vbCrLf + vbCrLf
        End Try

        Return (Nothing, ErrMessage)

    End Function

Posted by ipercube


© 2026 Ipercube Design