Visual Basic和Active Directory

时间:2015-04-17 17:16:59

标签: vb.net active-directory directoryservices

我在以下代码中遇到错误:

  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 = "domain.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://domain.local"
        Dim dirEntry As New DirectoryServices.DirectoryEntries(ldapPath, userName, password, 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

错误代码如下:

  

错误2重载解析失败,因为无法访问'新的'可以使用以下参数调用:       ' Public Sub New(过滤为字符串)':类型&System;目录服务.DirectoryEntries'的值无法转换为   '字符串&#39 ;.       ' Public Sub New(searchRoot As System.DirectoryServices.DirectoryEntry)':类型的值   ' System.DirectoryServices.DirectoryEntries'无法转换为   ' System.DirectoryServices.DirectoryEntry'

  

错误1键入' System.DirectoryServices.DirectoryEntries'没有建设者。

我的目标是让AD身份验证检查用户是否是特定AD组的成员。

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:0)

您的DirectoryEntries变量似乎正在使用DirectoryEntry类而不是dirEntriesDirectorySearcher没有构造函数允许它获取DirectoryEntries对象(它是DirectoryEntry个对象的集合)。

查看您正在使用的课程的文档。

https://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentries(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry(v=vs.110).aspx

Dim dirEntry As New DirectoryServices.DirectoryEntries(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)

应该是

Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, password, authenticationtypes.secure)
Dim dirSearcher As New DirectoryServices.DirectorySearcher(dirEntry)

答案 1 :(得分:0)

我相信您的dirEntry声明应如下:

Dim dirEntry As New DirectoryServices.DirectoryEntry(ldapPath, userName, userPassword, DirectoryServices.AuthenticationTypes.Secure)

答案 2 :(得分:0)

这里是修改后的整个代码

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)

    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



Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    Me.Close()
End Sub

Private Sub UsernameLabel_Click(sender As Object, e As EventArgs) Handles UsernameLabel.Click

End Sub

Private Sub form_login_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

结束班

现在,一旦点击“确定”,登录表单就不会继续。代码是否可能正在扫描活动目录并花费很长时间进行身份验证?或者更有可能我的身份验证后的帖子操作代码没有正确编码? -