如何访问LDAP服务器进行身份验证

时间:2012-12-05 09:58:47

标签: asp.net vb.net exception ldap

我在VB中编写了一个代码来访问LDAP服务器以进行身份​​验证。但是,它抛出一个异常,可能是nativeObject调用正在生成异常。动机是验证用户身份。我提供代码和例外。请帮我解决这个问题。

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

我有例外

  

System.Runtime.InteropServices.COMException(0x80005000):未知   错误(0x80005000)

     

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

     

在System.DirectoryServices.DirectoryEntry.Bind()

     

在System.DirectoryServices.DirectoryEntry.get_NativeObject()

     

at _Default.Button1_Click

请向我提供解决方案/修改代码为req。

1 个答案:

答案 0 :(得分:1)

DirectoryEntry

中缺少服务器名称

而不是

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

应该是

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