Last post Aug 04, 2014 05:31 AM by PatriceSc
Aug 03, 2014 11:25 AM|Sreenivasan Thangaraj|LINK
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;
string attributeList = GetAttributeFromXML(nodelist).Split(',');
System.Net.NetworkCredential networkCredential = null;
if (username == "")
networkCredential = new System.Net.NetworkCredential();
networkCredential = new System.Net.NetworkCredential(username, password, domainName);
LdapDirectoryIdentifier identifier = new LdapDirectoryIdentifier(domainName);
LdapConnection ldapConnection = new LdapConnection(identifier, networkCredential, AuthType.Kerberos);
SearchRequest searchRequest = new SearchRequest(GetURI(domainName), Filter, System.DirectoryServices.Protocols.SearchScope.Subtree, null);
PageResultRequestControl pageRequest = new PageResultRequestControl(pageSize);
SearchOptionsControl soc = new SearchOptionsControl(System.DirectoryServices.Protocols.SearchOption.DomainScope);
SearchResponse searchResponse = (SearchResponse)ldapConnection.SendRequest(searchRequest);
PageResultResponseControl pageResponse = (PageResultResponseControl)searchResponse.Controls;
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() + '"' + ",");
// If length is zero, there are no more pages to request
if (pageResponse.Cookie.Length == 0)
// 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)
catch (LdapException Exp)
catch (Exception Exp)
This works perfect two of our test environment.List of test environment
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
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.
Aug 04, 2014 05:31 AM|PatriceSc|LINK
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.