How to access LDAP Server for authentication

1

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)
        Response.Cookies.Add(cookie)
        Dim entry As New DirectoryEntry("LDAP://xyz.com/dc=xyz,dc=com", TextBox1.Text, TextBox2.Text)
        Try
            Dim obj As New Object
            obj = entry.NativeObject
            Dim search As New DirectorySearcher(entry)
            search.Filter = "(SAMAccountName=" + TextBox1.Text + ")"
            search.PropertiesToLoad.Add("cn")
            Dim result As SearchResult
            result = search.FindOne()
            If result.Equals(Nothing) then
                MsgBox("Try Again with valid username")
            Else
                MsgBox("User Found!")
            Response.Redirect("~/Dashboard.aspx")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        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.

asp.net
vb.net
exception
ldap
asked on Stack Overflow Dec 5, 2012 by snehal

1 Answer

1

Server name is missing in DirectoryEntry

Instead of

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

it should be

Dim entry As New DirectoryEntry("LDAP://SERVER-NAME/xyz.com/dc=xyz,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