Search code examples
vb.netactive-directorydirectoryservices

Visual Basic and Active Directory


I'm getting errors on the below code:

  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, "[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://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

The error codes are as follows:

Error 2 Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Sub New(filter As String)': Value of type 'System.DirectoryServices.DirectoryEntries' cannot be converted to 'String'. 'Public Sub New(searchRoot As System.DirectoryServices.DirectoryEntry)': Value of type 'System.DirectoryServices.DirectoryEntries' cannot be converted to 'System.DirectoryServices.DirectoryEntry'.

and

Error 1 Type 'System.DirectoryServices.DirectoryEntries' has no constructors.

My goal is to have AD authentication check if the user is a member of a particular AD group.

Any help would be greatly appreciated.


Solution

  • Looks like you are using the DirectoryEntries class instead of DirectoryEntry for your dirEntries variable. DirectorySearcher has no constructors to allow it to take a DirectoryEntries object (which is a collection of DirectoryEntry objects).

    Take a look at the documentation for the classes you are using.

    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)
    

    should be

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