0

I am attempting to authenticate users via LDAP.

I am able to query ldap and fill a listbox with users found in a certain OU.

When i try to authenticate via a user found in the above list, it returns my msgbox of "Failed to Login".

Here is my code:

This is put in a button function

Dim uid As String = txtusername.Text & ",OU=fake,OU=fake,DC=fake,DC=com"
Dim password As String = txtpassword.Text
Dim de As New DirectoryEntry("LDAP://fake.com/OU=fake,OU=fake,DC=fake,DC=com", uid, password, AuthenticationTypes.None)

Try
    Dim ds As DirectorySearcher = New DirectorySearcher(de)
    ds.FindOne()
    MsgBox("Login!")
Catch
    MsgBox("Fail!")
End Try

1 Answer 1

3

One of the DirectoryEntry constructor overloads allows you to specify the username and password. To see if the user name and password are correct try and create a directoryEntry object with the given username and password. If the password username combination are incorrect you will get an exception. Here is a sample function to authenicate a user.

 Public Function Authenicate(ByVal username As String, ByVal password As String) As Boolean
       Dim isValid As Boolean = False
    Try
       Dim de As New DirectoryServices.DirectoryEntry("LDAP://YourActiveDirectoryName", username, password, _
                DirectoryServices.AuthenticationTypes.Secure Or _
                DirectoryServices.AuthenticationTypes.Sealing Or _
                DirectoryServices.AuthenticationTypes.Signing)
        de.RefreshCache()
        isValid = True
    Catch
    End Try
    Return isValid
End Function
Sign up to request clarification or add additional context in comments.

2 Comments

thanks ken. how can i call that function in a button using my txtusername and txtpassword boxes?
Try something like if Authenicate(txtUserName.Text,txtPassword.Text) then MsgBox("Login") else MsgBox("Fail") end if

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.