Active Directory VB.NET

  • Thread starter Thread starter Adam J. O'Rourke
  • Start date Start date
A

Adam J. O'Rourke

HI I'm trying to write a Vb program to get some pieces of information
for our Active directory, but when i connect to my AD server, the only
information i can seem to get is the DNS information. I wrote the
following code to try and find what search filters i needed to get the
User information i wanted, but all i get back is the DNS information,
any help you can give me would be a huge help.

Dim strpath As String =
"LDAP://server.domain.com/DC=domain,DC=com"
Dim LogonName As String = "anonymous"
Dim LogonPassword As String = ""
Dim count As Int16

StopSearch = False

Dim myDE As New
System.DirectoryServices.DirectoryEntry(strpath, LogonName,
LogonPassword)
myDE.AuthenticationType = AuthenticationTypes.ReadonlyServer


Dim MySearch As New
System.DirectoryServices.DirectorySearcher(myDE)

MySearch.SearchRoot = myDE
MySearch.SearchScope = SearchScope.Subtree
txtOutput.Text = ""

Dim results As SearchResultCollection
Try
results = MySearch.FindAll

Dim res As SearchResult
Dim key, objValue
count = 0
For Each res In results
If StopSearch Then Exit For
For Each key In res.Properties.PropertyNames
If StopSearch Then Exit For
If UCase(key) = "DC" Then txtOutput.Text &= vbCrLf
& vbCrLf
txtOutput.Text &= key & " = "
Try
For Each objValue In res.Properties(key)
If StopSearch Then Exit For
txtOutput.Text &= objValue.ToString() &
vbCrLf
Next objValue
Catch
End Try
System.Windows.Forms.Application.DoEvents()
count += 1
If count = 10 Then Exit For
Next
Next
Catch
MsgBox("invalid search")
Exit Sub
End Try
MsgBox("finished search")


This code shows me alot of information, but not the information i
want.

Thank you,
Adam J. O'Rourke
Programer/Technician
 
HI I'm trying to write a Vb program to get some pieces of information
for our Active directory,

Comments inline with code.....

Dim strpath As String = "LDAP://server.domain.com/DC=domain,DC=com"

Tip: try to use serverless binding unless you have specific reason to
use the server: LDAP://dc=domain,dc=com should be fine

BIG TIP: add a "using System.DirectoryServices" to your file, then you
don't need to keep repeating that all over the place!
Dim LogonName As String = "anonymous"
Dim LogonPassword As String = ""
Dim myDE As New System.DirectoryServices.DirectoryEntry(strpath, LogonName, LogonPassword)

Tip: don't specify user name and password unless you really have to -
try using this instead

dim myDE as New DirectoryEntry(strpath)
Dim MySearch As New DirectorySearcher(myDE)
MySearch.SearchRoot = myDE

This is redundant - you've already specified "myDE" as your search
root by passing it in the constructor

YOu'll also need to specify something to search for - a filter. If you
want users, try something like this:

myDE.Filter = "(objectCategory=Person)"

That should help to get you started.

Also, you might want to subscribe to the microsoft.public.adsi.general
newsgroup - that's really where the AD / ADSI / LDAP mavens hang out!
:-)

Marc

================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top