2012-12-05 123 views
1

我已經在VB中編寫了一個代碼來訪問LDAP服務器以進行身份​​驗證。但是,它引發異常,可能nativeObject調用正在生成異常。動機是認證用戶。我提供的代碼和例外。請幫我解決這個問題。如何訪問LDAP服務器進行身份驗證

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)

在System.DirectoryServices.DirectoryEntry.Bind(布爾 throwIfFail)

at System.DirectoryServices.DirectoryEntry.Bind()

在System.DirectoryServices.DirectoryEntry.get_NativeObject()

在_Default.Button1_Click

請爲我提供解決方案/修改代碼REQ。

回答

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) 
+0

感謝維納亞克....我解決我的問題。 – snehal