Click here to Skip to main content
15,887,942 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to configure LDAP server and fetch the certificates from the same to test locally?
Tried below code but getting the exception that the server is not operational.

C#
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://some.ldap.server.com");
              rootEntry.AuthenticationType = AuthenticationTypes.None; //Or whatever it need be
              DirectorySearcher searcher = new DirectorySearcher(rootEntry);
              string searchString = "(CN = EFTS-CA-TEST)";
                          
              searcher.Filter = searchString;//Search how you want.  Google "LDAP Filter" for more.
              foreach (SearchResult result in searcher.FindAll())
              {
                  Console.WriteLine("account name: {0}", result.Properties["samaccountname"].Count > 0 ? result.Properties["samaccountname"][0] : string.Empty);
                  Console.WriteLine("common name: {0}", result.Properties["cn"].Count > 0 ? result.Properties["cn"][0] : string.Empty);
              }
Posted
Updated 26-Oct-15 18:37pm
v4

1 solution

Well, you dont tell us where the exception is coming from, so ...

I'd step through the code and check in particular to see if this

DirectoryEntry rootEntry = new DirectoryEntry("LDAP://some.ldap.server.com");


rootEntry is null after that call - you might need to specify that connection like

DirectoryEntry rootEntry = new DirectoryEntry("LDAP://some.ldap.server.com","userid","password");


it goes without saying, that these entries :-

"LDAP://some.ldap.server.com"
"userid"
"password"

must be correct for your LDAP/AD server (I hope you just changed 'some.ldap.server.com' for the purposes of displaying the code here)

btw this syntax is 'better' ie to release the connection and tidy up the directory entries

using (DirectoryEntry ...
{
...
}


You might also need to specify the LDAP Connection like this and/or supply a target OU from where to search "LDAP://CN=server,DC=domain,DC=com"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900