LDAP server could not be contacted when connecting using C# RRS feed

  • Question

  • I have written the below code to connect to LDAP server and validate the user credentials.

     public static string AuthFunction_One(string identity, string password, string containerString, string adServerName, bool useLDAPS, IdentityType identityType)
            string failedString = "FAILED";
            string successString = "SUCCESS";
            string returnValue = failedString;
                PrincipalContext ctx = null;
                ctx = new PrincipalContext(ContextType.Domain, "ldap://localhost:10389/dc=example,dc=com", "uid=rish,dc=example,dc=com");
                UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, identityType, identity);
                PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
                if (ctx.ValidateCredentials(identity, password))
                    return successString;
                    return failedString;
            catch (Exception ex)
                NLogHelper.GetInstance().Log("ADUtilityClass", "AuthFunction_One", NLog.LogLevel.Debug, "Error in function. Ex: " + ex.ToString());
                return failedString;

    This is throwing the below exception.

    Exception: Exception thrown: 
    'System.DirectoryServices.AccountManagement.PrincipalServerDownException' in System.DirectoryServices.AccountManagement.dll ("The server could not be contacted.")

    When I try to connect through LDAP explorer, it does connect.Below are the configurations I used there.

    userdn -> uid=rish,dc=example,dc=com
    basedn -> dc=example,dc=com
    password -> secret
    servername -> localhost
    port -> 10389
    version -> 3

    What am I doing wrong in my C# code? any help would be much appreciated.

    Monday, June 5, 2017 11:22 AM

All replies

  • Hi Rishanthan,

    Based on your description, I create a demo and reproduce your issue on my side, it seems that PrincipalContext works well with AD only. If your directory is OpenLDAP/ any other, then try below code:

                    LdapConnection ldapConnection;
                    string ldapServer = "ldap://localhost:389/dc=example,dc=com";
                    NetworkCredential credential = new NetworkCredential("username", "password", "domain"); 
                    // Create the new LDAP connection
                    ldapConnection = new LdapConnection(ldapServer);
                    ldapConnection.Credential = credential;
                    Console.WriteLine("LdapConnection is created successfully.");
                catch (Exception e)
                    Console.WriteLine("\r\nUnexpected exception occurred:\r\n\t" + e.GetType() + ":" + e.Message);

    Or use DirectoryEntry like this:

    DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://", "username", "password");

    Best regards,

    Cole Wu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, June 6, 2017 3:23 AM