LDAP connection doesn't work on Server

Last post 05-08-2008 9:10 AM by mschumacker. 1 replies.

Sort Posts:

  • LDAP connection doesn't work on Server

    05-07-2008, 10:21 AM

    All,

     I have a custom Membership Provider that is authenticating against an OpenLDAP server.  This works perfectly fine in development, but once I push this to our development servers, it doesn't work.  I keep getting the error:

     System.Runtime.InteropServices.COMException (0x8007203A): The server is not operational.

    Here's my code:

    Dim searchRoot As DirectoryEntry = New DirectoryEntry(LDAP://ldap-1.areaname.com/OU=People,DC=areaname,DC=com)
            searchRoot.Username = String.Format("uid={0},ou=People,dc=areaname,dc=com", username)
            searchRoot.Password = password
            searchRoot.AuthenticationType = AuthenticationTypes.SecureSocketsLayer

            Dim dirSearcher As DirectorySearcher = New DirectorySearcher(searchRoot)
            dirSearcher.Filter = String.Format("uid={0}", username)
            dirSearcher.SearchScope = DirectoryServices.SearchScope.Subtree
            Dim result As SearchResult = Nothing
            Dim
    theMessage As String = String.Empty


            Try
                result = dirSearcher.FindOne()
                If result IsNot Nothing Then
                    theMessage = "User Found"
                End If
            Catch
    ex As Exception
                theMessage = ex.ToString()
            End Try

            Return
    theMessage

     Again, this works fine on my local box, but not on the server.  I have installed the certificate from our LDAP server, which is an internal certificate.  If I look at the event log on the server, I see the following:

    Failed auto update retrieval of third-party root list sequence number from: <http://www.download.windowsupdate.com/msdownload/update/v3/static/trustedr/en/authrootseq.txt> with error: This network connection does not exist.

    Any ideas?  I've heard I might be better off using the System.DirectoryServices.Protocols namespace.  Thoughts on this?

     

  • Re: LDAP connection doesn't work on Server

    05-08-2008, 9:10 AM
    Answer

    Fixed it.  Luckily I found this article that explains the System.DirectoryServices.Protocols namespace.  Unfortunately there was a problem with the certificate issuer name and therefore my task kept failing. 

    The code that fixed it is below.  The main item was the VerifyServerCertificate portion where I could override the issue of having different names on the certificate issuer and the server I was connecting to.  That method is at the bottom of the listing:

     

    Dim ldi As LdapDirectoryIdentifier = New LdapDirectoryIdentifier(ldap-1.areaname.com)
            Dim myUserName As String = String.Format("uid={0},ou=People,dc=areaname,dc=com", username)
            Dim creds As System.Net.NetworkCredential = New System.Net.NetworkCredential(myUserName, password)
            Dim conn As LdapConnection = New LdapConnection(ldi, creds)
            Dim response As SearchResponse = Nothing
            Dim anEntry As SearchResultEntry = Nothing
            Dim isUserFound As Boolean = False
    
            conn.SessionOptions.ProtocolVersion = 3
            conn.SessionOptions.SecureSocketLayer = True
            conn.AuthType = AuthType.Basic
            conn.SessionOptions.VerifyServerCertificate = New VerifyServerCertificateCallback(AddressOf ServerCertificateRoutine)
            Dim filter As String = String.Format("uid={0}", username)
    
            Dim aRequest As SearchRequest = New SearchRequest("OU=People,DC=areaname,DC=com", filter, SearchScope.Subtree)
    
            Try
                conn.Bind()
                response = conn.SendRequest(aRequest)
                anEntry = response.Entries(0)
                If anEntry.Attributes.Count > 0 Then
                    isUserFound = True
                Else
                    isUserFound = False
                End If
            Catch ldapEx As LdapException
                'this is thrown when the connection fails
                isUserFound = False
            Catch ex As Exception
                isUserFound = False
                Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "General")
                If rethrow Then
                    Throw
                End If
            End Try
    
            Return isUserFound
      
     Private Shared Function ServerCertificateRoutine(ByVal conn As LdapConnection, ByVal cert As X509Certificate)
            'This ignores any errors from the certificate
            Return True
        End Function

     Hope this helps others who have hit this.

    Matt

Page 1 of 1 (2 items)