2017-10-10 38 views
0

下面是我嘗試通過VB.NET代碼連接到LDAP的代碼。下面的代碼在VS 2015中的本地PC上工作,但當我在服務器中託管相同的代碼時發生錯誤。LDAP連接無法在服務器IIS上工作,但在本地VS 2015中工作

我調用FindAll()函數的方式有問題嗎?這個相同的函數在我的本地PC中返回所需的值,但在服務器中給出了一個例外。我已抓獲來自服務器的異常,代碼

Private entry As DirectoryEntry = Nothing 

Public Enum ADProperties 
    distinguishedName 
    displayName 
    telephoneNumber 
    samAccountName 
    manager 
    title 
    department 
    givenName 
    sn 
End Enum 

entry = New DirectoryEntry("LDAP://DC=asia,DC=contoso,DC=com") 

Dim lstSearch As List(Of LDAPSearchResult) = New List(Of LDAPSearchResult) 

lstSearch = Search(ADProperties.samAccountName, parts(1).ToString()) 

       If Not lstSearch Is Nothing AndAlso lstSearch.Count > 0 Then 
        For Each a As LDAPSearchResult In lstSearch 
         email = a.UserPrincipalName 
         userid = a.ComputerUserId 
         name = a.GivenName 
         Exit For 
        Next 

Public Function Search([property] As ADProperties, propertyValue As [String]) As List(Of LDAPSearchResult) 
    Dim lstSearchResults As New List(Of LDAPSearchResult)() 

    Try 
     Dim search__1 As New DirectorySearcher(entry) 
     Dim resultCollection As SearchResultCollection 


     LoadProperties(search__1) 

     search__1.Filter = String.Concat("(", [property].ToString(), "=", propertyValue, ")") 

     resultCollection = search__1.FindAll() //Exception is caught here 

     If resultCollection IsNot Nothing Then 
      For Each result As SearchResult In resultCollection 
       Dim objSearchResult As New LDAPSearchResult() 

       MapToObject(result, objSearchResult) 

       lstSearchResults.Add(objSearchResult) 
      Next 
     End If 

    Catch ex As Exception 
     Throw New Exception(ex.Message.ToString()) 
    End Try 

    Return lstSearchResults 
End Function 

Private Sub MapToObject(result As SearchResult, ByRef objSearchResult As LDAPSearchResult) 
    Try 

     If result.Properties("title").Count > 0 Then 
      objSearchResult.Title = result.Properties("title")(0).ToString() 
     End If 
     If result.Properties("distinguishedName").Count > 0 Then 
      objSearchResult.distinguishedName = result.Properties("distinguishedName")(0).ToString() 
     End If 
     If result.Properties("displayName").Count > 0 Then 
      objSearchResult.displayName = result.Properties("displayname")(0).ToString() 
     End If 
     If result.Properties("telephoneNumber").Count > 0 Then 
      objSearchResult.telephoneNumber = result.Properties("telephoneNumber")(0).ToString() 
     End If 
     If result.Properties("samAccountName").Count > 0 Then 
      objSearchResult.samAccountName = result.Properties("samAccountName")(0).ToString() 
     End If 

     If result.Properties("department").Count > 0 Then 
      objSearchResult.department = result.Properties("department")(0).ToString() 
     End If 
     If result.Properties("givenName").Count > 0 Then 
      objSearchResult.FirstName = result.Properties("givenName")(0).ToString() 
     End If 
     If result.Properties("sn").Count > 0 Then 
      objSearchResult.LastName = result.Properties("sn")(0).ToString() 
     End If 
    Catch ex As Exception 
     ex.Message.ToString() 
    End Try 
End Sub 

錯誤後粘貼了以下錯誤消息指出如下

System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
System.DirectoryServices.DirectoryEntry.Bind() 
System.DirectoryServices.DirectoryEntry.get_AdsObject() 
System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) 

請幫

+0

顯示完整的異常。 –

+0

下面是在System.DirectoryServices.DirectoryEntry.Bind(布爾throwIfFail) 完整的例外 在System.DirectoryServices.DirectoryEntry.Bind() 在System.DirectoryServices.DirectoryEntry.get_AdsObject() 在System.DirectoryServices.DirectorySearcher。 FindAll(布爾findMoreThanOne) 在.Search(ADProperties屬性,字符串propertyValue)] – ssuhas76

+0

您錯過了異常的消息正文!請將第一行添加到您的代碼中 - 您顯示的是異常堆棧跟蹤,無例外消息! –

回答

0

解決的問題通過在添加下面的代碼web.config

<identity impersonate="false"> 
相關問題