VB登录表单不会继续

时间:2015-04-17 19:03:45

标签: vb.net active-directory directoryservices

我无法获取VB登录表单。

Public Class form_login



Private Function AuthenticateUser() As Boolean
    Dim username As String = txtbok_login_username.Text
    Dim password As String = txtbox_login_password.Text
    Dim domain As String = "patten.local"

    Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password, "Admins@WokasCustomer.com")

    Return isAuthenticated
End Function




Public Function ValidateActiveDirectoryLogin(ByVal domainName As String, ByVal userName As String, ByVal userPassword As String, ByVal groupName As String) As Boolean
    Dim isValidated As Boolean = False

    Try

        Dim ldapPath As String = "LDAP://patten.local"
        Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, userPassword, DirectoryServices.AuthenticationTypes.Secure)
        Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)



        dirSearcher.Filter = "(userPrincipalName=" & userName & ")"
        dirSearcher.PropertiesToLoad.Add("memberOf")

        Dim result As DirectoryServices.SearchResult = dirSearcher.FindOne()

        If Not result Is Nothing Then

            If groupName.Length = 0 Then
                isValidated = True
            Else
                Dim groupCount As Integer = result.Properties("Fiserv Processing - MIS").Count
                Dim isInGroup As Boolean = False

                For index As Integer = 0 To groupCount - 1
                    Dim groupDN As String = result.Properties("Fiserv Processing - MIS").Item(index)

                    Dim equalsIndex As Integer = groupDN.IndexOf("=")
                    Dim commaIndex As Integer = groupDN.IndexOf(",")

                    Dim group As String = groupDN.Substring((equalsIndex + 1), (commaIndex - equalsIndex) - 1).ToLower
                    If group.Equals(groupName.ToLower) Then
                        isInGroup = True
                        Exit For
                    End If
                Next index

                isValidated = isInGroup
            End If
        End If
    Catch ex As Exception
        Throw New Exception(ex.Message)
    End Try

    Return isValidated

End Function

我的代码中缺少什么?

我正在尝试将用户身份验证到活动目录组。一旦我在登录表单上输入我的用户名和密码,然后单击“确定”,表单就会无需继续。我错过了邮政程序代码吗?

1 个答案:

答案 0 :(得分:0)

看起来我们在互联网上的某个地方查看了相同的指南...这是我在我的解决方案中实施的内容,它的工作没有问题:

Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal Username As String, ByVal Password As String) As Boolean

    Dim Success As Boolean = False
    Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, Username, Password)
    Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
    Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel

    Try
        Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne
        Success = Not (Results Is Nothing)
    Catch
        Success = False
    End Try

    Return Success
End Function

以下是我调用函数的方法:

If ValidateActiveDirectoryLogin(Environment.UserDomainName, Environment.UserName, passBox.Text) Then
   ' Success, you can proceed
   ' continueDoingSomething()
Else
   ' Failure, go do something else
   ' failedLogin()
End If