LDAP Bind Password

S Abijith 466 Reputation points
2023-06-05T11:02:11.7833333+00:00

Hi all,

We have a windows application built on .Net Framework 4.7.2. In this application, we try to authenticate a user using LDAP authentication. When we try to authenticate a user, it says authentication failed.

We have another third party application which also authenticates using LDAP. If we use the same credentials that we used for the windows application (mentioned above), authentication succeeds.

There is one difference between the windows application and the third party application. The third party application makes use of 'Bind Password' which is not used in the windows application.

So the question is, what is the use of this 'Bind Password' which is provided in addition to the user credentials. Does anybody know as to how to use this 'Bind Password'??

Any help is appreciated!!

private string AuthenticateUser(string username, string password, ref SearchResultEntry results)
{
    string errorMessage = string.Empty;
    System.DirectoryServices.Protocols.LdapConnection ldapConnection = null;
    try
    {
        // Create the new LDAP connection
        LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier(Convert.ToString(CoreData.SnmpcToolkit.LdapServerIPAddress), 389);
        ldapConnection = new System.DirectoryServices.Protocols.LdapConnection(ldi);
        ldapConnection.AuthType = AuthType.Basic;
        NetworkCredential nc = new NetworkCredential("CN=" + username + "," + CoreData.SnmpcToolkit.LdapDistinguishedName, password);

        ldapConnection.Timeout = new TimeSpan(0, 0, 20);
        ldapConnection.Bind(nc);

        SearchRequest request = new SearchRequest(CoreData.SnmpcToolkit.LdapDistinguishedName, "(sAMAccountName=" + username + ")", System.DirectoryServices.Protocols.SearchScope.Subtree);
        SearchResponse response = (SearchResponse)ldapConnection.SendRequest(request);

        if (response.Entries.Count == 1)
        {
            results = response.Entries[0];
        }
    }
            
    catch (Exception ex)
    {
         errorMessage = ex.Message;
    }

    return errorMessage;
}

Thank you in advance!!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,190 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Osjaetor 480 Reputation points
    2023-06-05T17:27:02.0166667+00:00

    Hi S Abijith,

     This is the code I use for LDAP authentication, could you test it to see if it works for you?
    
    using System.ComponentModel;
    using System.Data.SqlClient;
    using System.DirectoryServices;
    using System.DirectoryServices.AccountManagement;
    
    public class Auth
    {
        private bool AuthenticateUser(string user, string pass)
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://yourdomain.com", user, pass, AuthenticationTypes.Secure);
            try
            {
                DirectorySearcher ds = new DirectorySearcher(de);
                ds.FindOne();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
    

    Regards,


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.