2015-04-17 50 views
0

我无法让我的VB登录窗体继续。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, "[email protected]") 

    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 

我在代码中缺少什么?

我在尝试将用户身份验证到活动目录组。一旦我在登录表单上输入了我的用户名和密码,然后单击“确定”,表单就会处于不执行状态。我是否缺少后期程序代码?

回答

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