Active Directory search in VB.NET

Hello

I'm looking for a working example...

VB.NET search for a specific email in Active Directory

Thanks

Mike
LVL 1
mikeydkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
Take a look at the following snippet: http://forums.asp.net/t/1380245.aspx
Dim strValue As String
            
            Try
                
                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
 
       
           
                objDirSearcher.Dispose()
 
                objCollSearchResult.Dispose()
 
                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

0
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.

Chris
' 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
    Console.WriteLine(adResult.Properties("name").Item(0).ToString())
    Console.WriteLine(adResult.Properties("mail").Item(0).ToString())
Next

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.