Solved

LDAP, ASP.NET, and VB. Can't populate dropdownlist with email addresses from exchange server.

Posted on 2006-10-23
6
257 Views
Last Modified: 2012-06-21
I have tried several examples but can't seem to get the right sequence of pieces of code to make it happen. Any suggestions on how to do this.
0
Comment
Question by:wrwiii12
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:bigphuckinglizard
Comment Utility
Converted from c# so may be a bit funky, but try this... you also need to make sure you have a reference to System.DirectoryServices in your project.

               Dim LDAPConnection As New DirectoryEntry("LDAP://domain/OU=yourou,DC=domain,DC=tld", ConfigurationManager.AppSettings("LDAPUsername"), ConfigurationManager.AppSettings("LDAPPassword"))
                Try
                    Dim Searcher As New DirectorySearcher(LDAPConnection)
                    Searcher.Filter = "(objectclass=user)"
                    Searcher.PropertiesToLoad.Add("mail")
                    Dim results  as SearchResultCollection = Searcher.FindAll()
                    For Each (result as SearchResult in results) {
                        Dim email as String= LDAPConnection.Properties("mail")[0].ToString()
                        dropdownlist1.items.add(new ListItem(email))
                    Next
                Finally
                    LDAPConnection.Close() //very bad things happen if connections to ad are left open!!!
                End Try
0
 
LVL 4

Author Comment

by:wrwiii12
Comment Utility
Almost there! (I think)
Name 'result' is not declared.

I know what the domain is, but what are the other parameters OU, DC, and DC.
If my domain is called "ecc.dept.local", how should the string look?
0
 
LVL 4

Author Comment

by:wrwiii12
Comment Utility
Here is the revised code that at least compiles. It runs but throws and exception
at the LDAPConnection.Properties line:
Index was out of range. Must be non-negative and less than the size of the collection.


Dim LDAPConnection As New DirectoryEntry("LDAP://eccdept.local",
ConfigurationManager.AppSettings("LDAPUsername"),
ConfigurationManager.AppSettings("LDAPPassword"))
        Try
            Dim Searcher As New DirectorySearcher(LDAPConnection)
            Searcher.Filter = "(objectclass=user)"
            Searcher.PropertiesToLoad.Add("mail")
            Dim result As SearchResult
            Dim results As SearchResultCollection = Searcher.FindAll()
            For Each result In results
                Dim email As String  <--(seperated it for clarity on my part) -->
                email = LDAPConnection.Properties("mail")(0).ToString()  <-- line it errors on -->
                DropDownList1.Items.Add(New ListItem(email))
            Next
        Finally
            LDAPConnection.Close() '//very bad things happen if connections to ad are left open!!!
        End Try
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 6

Expert Comment

by:bigphuckinglizard
Comment Utility
you need to know the netbios name of your domain as well, it should be

Dim LDAPConnection As New DirectoryEntry("LDAP://NETBIOSNAME/dc=eccdept,dc=local", username, password)

and i think i've found that vb needs an ienumerator in the loop:

            For Each result In results
                Dim email As String  <--(seperated it for clarity on my part) -->
                Dim i As IEnumerator = LDAPConnection.Properties("mail").GetEnumerator()                  
                While i.MoveNext
                        DropDownList1.Items.Add(i.Current.ToString())
                  End While
            Next
0
 
LVL 4

Author Comment

by:wrwiii12
Comment Utility
it executes the results.count which the mouse tooltip says that there are 328 in count
it passes on through the Dim i as IENumerator and starts to cycle through the while i.movenext
it never drops inside the while loop
0
 
LVL 6

Accepted Solution

by:
bigphuckinglizard earned 200 total points
Comment Utility
i've loaded up vb and got it sussed

Dim LDAPConnection As New DirectoryEntry("LDAP://NETBIOSNAME/dc=eccdept,dc=local", username, password)

        Try
            Dim Searcher As New DirectorySearcher(LDAPConnection)
            Searcher.Filter = "(objectclass=user)"
            Searcher.PropertiesToLoad.Add("mail")
            Dim result As SearchResult
            Dim results As SearchResultCollection = Searcher.FindAll()
            For Each result In results
                If result.Properties.Contains("mail") Then
                    DropDownList1.Items.Add(result.Properties("mail")(0).ToString())
                End If
            Next
        Finally
            LDAPConnection.Close() '//very bad things happen if connections to ad are left open!!!
        End Try
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now