By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,966 Members | 1,488 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,966 IT Pros & Developers. It's quick & easy.

Code Check: Active Directory

P: n/a
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)(samacco untname=" & 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").Ite m(0).ToString)

Catch ex As System.NullReferenceException

Me.SetGivenName("")

End Try

' exception handling
Try

Me.SetSurname(Result.Properties("sn").Item(0).ToSt ring)

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
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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?
Nov 19 '05 #2

P: n/a
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
Nov 19 '05 #3

P: n/a
Fixed it!
Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.