• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1127
  • Last Modified:

Active Directory search in VB.NET


I'm looking for a working example...

VB.NET search for a specific email in Active Directory


2 Solutions
Take a look at the following snippet: http://forums.asp.net/t/1380245.aspx
Dim strValue As String
                Dim Domain As String = "mydomain.local"
                Dim Username As String = textname.Text
                Dim Password As String = textpassword.Text
                Dim strPath As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, Username, Password)
                Dim objDirSearcher As New System.DirectoryServices.DirectorySearcher(strPath)
                Dim objCollSearchResult As System.DirectoryServices.SearchResultCollection
                Dim objlSearchResult As System.DirectoryServices.SearchResult
                Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection
                Dim objCollResultPropertyValue As System.DirectoryServices.ResultPropertyValueCollection
                objDirSearcher.Filter = ("(&(objectClass=user)(samaccountname=" & Username & "))")
                objCollSearchResult = objDirSearcher.FindAll()
                Select Case (objCollSearchResult.Count)
                    Case 0
                        strValue = ""
                    Case Is > 1
                        'Exit Function
                    Case Is = 1
                        objlSearchResult = objCollSearchResult.Item(0)
                        objCollResultProperty = objlSearchResult.Properties
                        objCollResultPropertyValue = objCollResultProperty.Item("mail")  'Put in the Active Directory object name that you wish to retrieve here.
                        strValue = objCollResultPropertyValue.Item(0)
                        lblResult.Text = strValue
                End Select
                objlSearchResult = Nothing
                objCollResultProperty = Nothing
                objCollResultPropertyValue = Nothing
            Catch ex As System.Exception
                Dim strMess As String
                strMess = ex.Message
                lblResult.Text = "You did not authenticate successfully"
                strValue = ""
            End Try

Open in new window

Chris DentPowerShell DeveloperCommented:

This snippet demonstrates how to use the Directory Searcher and an Ldap Filter to find a particular e-mail address in Active Directory.

' You will need to add a reference to this as well
Imports System.DirectoryServices
' Snip
' ...
' Proxy addresses is a multi-value field. Each SMTP address is prefixed with "smtp:"
' hence the inclusion of the Wildcard, *.
Dim ldapFilter As String = "(proxyAddresses=*user@domain.com)"
' If only the Primary Email address is required for the search this is more efficient
' Dim ldapFilter As String = "(mail=user@domain.com)"
' If we are authenticated and running this from a domain member all that needs
' to be done to connect to the current domain is this:
Dim adDomain As New DirectoryEntry
' Create the search
Dim adSearch As New DirectorySearcher(adDomain, ldapFilter)
' Enable Paging and set the Page Size (if more than 1000 results are necessary)
adSearch.PageSize = 1000
' Return a limited set of properties for the object
adSearch.PropertiesToLoad.AddRange(New String() {"name", "distinguishedName", "mail"})
' Return the results
Dim adResults As SearchResultCollection = adSearch.FindAll()
' Loop through the results
For Each adResult As SearchResult In adResults

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now