2017-10-10 37 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"> 
相关问题