Share via


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

Question

Friday, August 1, 2014 1:19 PM

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 

Scenario 3 -- Here we face Issue

Above code works only for domain name,If we give Domain controller or IP address it throws the same error

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.

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

All replies (1)

Friday, August 1, 2014 3:58 PM

Incorrect DNS settings(assure to use only domain internal DNS servers on the NICs) or firewalls blocking AD required traffic http://technet.microsoft.com/en-us/library/dd772723(WS.10).aspx