Code Check: Active Directory

Discussion in 'ASP .Net' started by Rob Meade, Nov 4, 2005.

  1. Rob Meade

    Rob Meade Guest

    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! :eek:)

    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
    Rob Meade, Nov 4, 2005
    #1
    1. Advertising

  2. Rob Meade

    Rob Meade Guest

    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?
    Rob Meade, Nov 4, 2005
    #2
    1. Advertising

  3. Rob Meade

    Rob Meade Guest

    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
    Rob Meade, Nov 4, 2005
    #3
  4. Rob Meade

    Rob Meade Guest

    Fixed it!
    Rob Meade, Nov 6, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SklNLkgu?=

    Q: check users in Active Directory

    =?Utf-8?B?SklNLkgu?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    496
    =?Utf-8?B?SklNLkgu?=
    Mar 21, 2005
  2. Caspy
    Replies:
    3
    Views:
    3,562
    Sean M
    Aug 4, 2005
  3. rote
    Replies:
    2
    Views:
    479
  4. Filip De Backer

    Check the username and password with the active directory

    Filip De Backer, Aug 26, 2005, in forum: ASP .Net Security
    Replies:
    3
    Views:
    305
    Filip De Backer
    Aug 29, 2005
  5. Craig Vedur
    Replies:
    5
    Views:
    651
Loading...

Share This Page