使用Active Directory Visual Basic中的名字/姓氏填充文本框

时间:2014-05-08 21:43:40

标签: vb.net active-directory

在过去的两天里,我一直在努力解决这个问题。就在我以为我弄明白的时候,不,不,哈哈。所以这是我的困境,我有一个表单,当用户输入用户ID时,2个文本框将填充其给定名称/ SurName。我有我的代码,以便它将连接到LDAP并验证UserID是否正确。我遇到问题的地方是在文本框中添加名称。

这是连接到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

以下是用户点击验证时按钮的代码:

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

任何帮助都会非常感激。干杯

2 个答案:

答案 0 :(得分:0)

您应该.PropertiesToLoad.Add("GN")更改.PropertiesToLoad.Add("givenName")必须在ADSI中使用正确的属性名称。

然后你应该通过引用返回结果(我多年没使用VB了,所以我希望有人会评论语法):

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

然后你应该用:

来调用它
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

答案 1 :(得分:0)

如果有人有同样的问题,请弄清楚:

这基本上做的是在我输入用户ID后,点击它,它用姓氏的名字填充接下来的两个字段。基本上你可以向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