Question

Man I've been killing myself trying to figure this out for the past 2 days. Just when i thought i had it figured out, nope, lol. So here's my dilemna, I have a form that when a user puts in a user ID, 2 text boxes are to be filled with their Given Name / SurName. I have my code so that it will connect to the LDAP and verify whether or not the UserID is correct. Where i'm having problems is adding the names to the text boxes.

This is the boolean that connects to AD:

Public Shared Function UserExists(ByVal username As String) As Boolean
    Dim answer As Boolean = False
    Dim dirEntry As DirectoryEntry = Nothing
    Dim ldapPath As String = "LDAP://(Insert LDAP nonsense here)"
    Dim dirSearcher As DirectorySearcher = Nothing
    Dim result As SearchResult = Nothing

    Try
        dirEntry = New DirectoryEntry(ldapPath)

        dirSearcher = New DirectorySearcher(dirEntry)
        With dirSearcher
            .Filter = "(CN=" & username & ")"
            .PropertyNamesOnly = True
            .PropertiesToLoad.Add("Name")
            .PropertiesToLoad.Add("GN")
            .PropertiesToLoad.Add("SN")

            result = .FindOne()

        End With
        If Not result Is Nothing Then
            answer = True
        End If
    Catch ex As Exception
        Throw New Exception(ex.Message)
    Finally

        dirEntry = Nothing
        dirSearcher = Nothing
    End Try
    Return answer

End Function

Here's the code for the button when the user hits verify:

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    Dim userID As String = TextBox18.Text



   If UserExists(userID) Then
        MsgBox("WooHoo")
    Else
        MsgBox("Fail")
    End If
   Textbox16.text = SN
   Textbox17.text = GN

Any help would be GREATLY appreciated. Cheers

Was it helpful?

Solution 2

Figured it out in case anyone ever have the same question:

What this essentially does is after I put in the User ID, and click away, it populates the next 2 fields with the first name last name. essentially you can add the same thing to the Button

Private Sub TextBox3_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus

    Dim deSystem As New DirectoryEntry("LDAP:")
    Dim dsSystem As New DirectorySearcher(deSystem)
    Dim srsystem As SearchResult

    If TextBox3.Text = Nothing Then

        Exit Sub
    Else

        Try

            dsSystem.Filter = "sAMAccountName=" & TextBox3.Text

            dsSystem.PropertiesToLoad.Add("mail") 'email address
            dsSystem.PropertiesToLoad.Add("department") 'dept
            dsSystem.PropertiesToLoad.Add("physicalDeliveryOfficeName") 'office
            dsSystem.PropertiesToLoad.Add("title") 'title, eg programmer1
            dsSystem.PropertiesToLoad.Add("telephoneNumber") 'phone
            dsSystem.PropertiesToLoad.Add("streetAddress") 'street address
            dsSystem.PropertiesToLoad.Add("l") 'city
            dsSystem.PropertiesToLoad.Add("st") 'state
            dsSystem.PropertiesToLoad.Add("postalCode") 'zip code
            dsSystem.PropertiesToLoad.Add("EmployeeId") 'empid 
            dsSystem.PropertiesToLoad.Add("givenName") '//first name from active directory
            dsSystem.PropertiesToLoad.Add("sn") '//lastname from active directory

            srsystem = dsSystem.FindOne()


            TextBox1.Text = srsystem.Properties("givenName").Item(0).ToString
            TextBox2.Text = srsystem.Properties("sn").Item(0).ToString
        Catch ex As Exception
            MsgBox("Invalid UserID")
        End Try
    End If

End Sub

OTHER TIPS

You should change .PropertiesToLoad.Add("GN") by .PropertiesToLoad.Add("givenName") The right attribute name must be use in ADSI.

Then you shoul return resul by reference something like (I do not use VB for years now, so I hope somebody will comment the syntax):

Public Shared Function UserExists(ByVal username As String, ByRef result As SearchResult) As Boolean
    Dim answer As Boolean = False
    Dim dirEntry As DirectoryEntry = Nothing
    Dim ldapPath As String = "LDAP://(Insert LDAP nonsense here)"
    Dim dirSearcher As DirectorySearcher = Nothing

    Try
        dirEntry = New DirectoryEntry(ldapPath)

        dirSearcher = New DirectorySearcher(dirEntry)
        With dirSearcher
            .Filter = "(CN=" & username & ")"
            .PropertyNamesOnly = True
            .PropertiesToLoad.Add("Name")
            .PropertiesToLoad.Add("givenName")
            .PropertiesToLoad.Add("SN")

            result = .FindOne()

        End With
        If Not result Is Nothing Then
            answer = True
        End If
    Catch ex As Exception
        Throw New Exception(ex.Message)
    Finally

        dirEntry = Nothing
        dirSearcher = Nothing
    End Try
    Return answer

End Function

Then you should call it with :

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    Dim userID As String = TextBox18.Text

   Dim result As SearchResult = Nothing

   If UserExists(userID, result) Then
        MsgBox("WooHoo")
    Else
        MsgBox("Fail")
    End If
   Textbox16.text = result.sn
   Textbox17.text = result.givenName
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top