How to access LDAP Server for authentication


I've written a code in VB to access LDAP server for authentication purpose. However, it is throwing an exception, probably nativeObject call is generating the exception. The motive is to authenticate the user. I'm providing the code and the exception. Kindly help me to resolve this issue.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cookie As HttpCookie = New HttpCookie("username")
        cookie.Value = TextBox1.Text
        cookie.Expires = DateAndTime.Now.AddHours(12)
        Dim entry As New DirectoryEntry("LDAP://,dc=com", TextBox1.Text, TextBox2.Text)
            Dim obj As New Object
            obj = entry.NativeObject
            Dim search As New DirectorySearcher(entry)
            search.Filter = "(SAMAccountName=" + TextBox1.Text + ")"
            Dim result As SearchResult
            result = search.FindOne()
            If result.Equals(Nothing) then
                MsgBox("Try Again with valid username")
                MsgBox("User Found!")
            End If
        Catch ex As Exception
        End Try

    End Sub

The exception i've got

System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)

at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)

at System.DirectoryServices.DirectoryEntry.Bind()

at System.DirectoryServices.DirectoryEntry.get_NativeObject()

at _Default.Button1_Click

Please provide me with the solution/modify the code as req.
asked on Stack Overflow Dec 5, 2012 by snehal

1 Answer


Server name is missing in DirectoryEntry

Instead of

Dim entry As New DirectoryEntry("LDAP://,dc=com", TextBox1.Text, TextBox2.Text)

it should be

Dim entry As New DirectoryEntry("LDAP://SERVER-NAME/,dc=com", TextBox1.Text, TextBox2.Text)
answered on Stack Overflow Dec 7, 2012 by Vinayak Pahalwan • edited Dec 7, 2012 by j0k

User contributions licensed under CC BY-SA 3.0