locked
The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable RRS feed

  • Question

  • User720013681 posted

    Hi Team,

    I am trying to extract the user and contact details from ActiveDirectory.

    I am using System.DirectoryServices.Protocols for get the data from the active directory. Below am pasting my code which retrieve the data from ActiveDirectory

     private void GetUserandComputerDetailsLDAP(StreamWriter csvWriter, string rootDSE, string domainName, XmlNodeList nodelist, string username, string password)
            {
                string Filter = "(|(objectClass=User)(objectclass=contact))";
    
                int pageSize = 10;
    
                try
                {
                    string[] attributeList = GetAttributeFromXML(nodelist).Split(',');
    
                    System.Net.NetworkCredential networkCredential = null;
    
                    if (username == "")
                        networkCredential = new System.Net.NetworkCredential();
                    else
                        networkCredential = new System.Net.NetworkCredential(username, password, domainName);
    
                    LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(domainName);
    
                    LdapConnection ldapConnection = new LdapConnection(identifier, networkCredential, AuthType.Kerberos);
    
                    ldapConnection.Bind();
    
                    SearchRequest searchRequest = new SearchRequest(GetURI(domainName), Filter, System.DirectoryServices.Protocols.SearchScope.Subtree, null);
    
                    PageResultRequestControl pageRequest = new PageResultRequestControl(pageSize);
    
                    searchRequest.Controls.Add(pageRequest);
    
                    SearchOptionsControl soc = new SearchOptionsControl(System.DirectoryServices.Protocols.SearchOption.DomainScope);
    
                    searchRequest.Controls.Add(soc);
                    while (true)
                    {
                        SearchResponse searchResponse = (SearchResponse)ldapConnection.SendRequest(searchRequest);
    
                        PageResultResponseControl pageResponse = (PageResultResponseControl)searchResponse.Controls[0];
    
                        foreach (SearchResultEntry entry in searchResponse.Entries)
                        {
                            StringBuilder sbcompleteRow = new StringBuilder();
    
                            SearchResultAttributeCollection attributeCollection = entry.Attributes;
    
                            foreach (string attribute in attributeList)
                            {
                                if (attributeCollection[attribute] != null && attributeCollection[attribute].Count > 0 && attributeCollection[attribute][attributeCollection[attribute].Count - 1].ToString() != "")
                                {
                                    sbcompleteRow.Append('"' + attributeCollection[attribute][attributeCollection[attribute].Count - 1].ToString() + '"' + ",");
    
                                }
                                else
                                {
                                    sbcompleteRow.Append(",");
                                }
    
                            }
                            csvWriter.WriteLine(sbcompleteRow.ToString());
                            Console.Write(".");
    
                        }
    
                        // If length is zero, there are no more pages to request
                        if (pageResponse.Cookie.Length == 0)
                            break;
    
                        // set the cookie of the pageRequest equal to the cookie of the pageResponse to request the next page of data
                        pageRequest.Cookie = pageResponse.Cookie;
                    }
    
                }
                catch (DirectoryOperationException Exp)
                {
                    MessageBox.Show(Exp.Message);
                }
                catch (LdapException Exp)
                {
                    MessageBox.Show(Exp.Message);
                }
                catch (Exception Exp)
                {
                    MessageBox.Show(Exp.Message);
                }
    
            }

    This works perfect two of our test environment.List of test environment

    1.Development.Testing.com

    2.ExchangeSRV.com

    3.Stout.Testing.com

    4.turtle.Emaileconomics.com

    Scenario 1

    Suppose our application runs in Development.Testing.com then

    we try to get the data for  the domain domain Development.Testing.com,Stout.Testing.com works fine.

    Even we could get the data from turtle.Emaileconomics.com which is outside the Testing forest .

    ExchangeSRV has no connection between these domain so we could not do that ,so that is not the issue.

    So in this environment we face no issue every thing was perfect.

    Scenario 2 - Here we face issue

    Now my application is running in Stout.Permessa.com then

    we try to get the data for  the domain domain Development.Testing.com,Stout.Testing.com works fine.

    But when we try to get the data from turtle.Emaileconomics.com which is outside the Testing forest  we are getting isssue 

    The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable

    But from the same machine we could connect to the domain turtle.Emaileconomics.com through ADSIedit and CSVDE.

    Now I am in confusion state and what might be the issue .

    Need your great help to step out of this issue.

    Scenario 3 - Here we face issue

    Above code is working only for domain name ,when I try to give the domin controller name or IP Address it is giving me the same error even in my environment

    System.DirectoryServices.AccountManagement.PrincipalServerDownException: The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable.

    Sunday, August 3, 2014 11:25 AM

Answers

  • User753101303 posted

    Hi,

    Case #2 is not the same machine than case #1? Could it be just blocked by a firewall? For now it seems an admin forum might be better than a programming forum as it seems rather a network/domain configuration issue rather than reallly a programming issue.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 4, 2014 5:31 AM