Code Check: Active Directory

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I have some code which I cobbled together from some examples online, so its
probably wrong, although I did have it working a little while ago,
unfortunately for some reason it seems to have stopped working..nothing's
change infrastructure wise, the only thing that has changed is that my
project has been converted to VS2005 - I'm not suggesting this has caused a
problem - just stating its the only change (that I can remember!)...

The code (snipped from the class):

Public Sub Search(ByVal samaccountname As String, ByVal activeDirectory As
String)

' declare variables
Dim DirectoryEntry As DirectoryEntry = New
DirectoryEntry(LDAP://myActiveDirectory)
Dim DirectorySearcher As DirectorySearcher = New
DirectorySearcher(DirectoryEntry)
Dim Results As DirectoryServices.SearchResultCollection
Dim Result As DirectoryServices.SearchResult
Dim ResultPropertyCollection As ResultPropertyCollection
Dim ResultProperty As String

' define search properties
With (DirectorySearcher)
.Filter =
"(&(objectClass=User)(objectCategory=User)(samaccountname=" & samaccountname
& "))"
.SearchScope = SearchScope.Subtree
End With

' search
Results = DirectorySearcher.FindAll

' check number of results
If Results.Count > 0 Then

' iterate through results
For Each Result In Results

' exception handling
Try

' populate collection
ResultPropertyCollection = Result.Properties

' set class variables
Me.SetRawData(RawData)

' exception handling
Try

Me.SetGivenName(Result.Properties("givenname").Item(0).ToString)

Catch ex As System.NullReferenceException

Me.SetGivenName("")

End Try

' exception handling
Try

Me.SetSurname(Result.Properties("sn").Item(0).ToString)

Catch ex As System.NullReferenceException

Me.SetSurname("")

End Try

End If

End Sub


This is a cut down version of what I'm currently using, but only in that I
was pulling more properties out..

Can anyone spot anything which would prevent the search from working? I'm
not getting any errors, but equally I'm not getting any values out of the ad
either - really frustrating as I was dead impressed with myself when I had
it working originally! :o)

Also, I'm sure this is nothing to do with it but are there any specific
ports that would need to be open on a LAN to enable this? I'd imagine I'd
have received an error somewhere if so - and I'm pretty sure that there
haven't been any infrastructure changes since it was working..

Regards

Rob
 
Oh, I should add, it seems to be that where I test for the results > 0 - it
never is, its always 0....but as I mentioned, no error?
 
Update:

I just ran my old .exe written in .net 1.1 and it fires up and searches
without any problems at all? Anyone got any ideas - it appears to only be a
problem because of .net 2.0

Regards

Rob
 
Back
Top