如何使用C#连接到LDAP服务器

时间:2020-07-02 06:57:38

标签: c# ldap

我试图按以下代码测试与LDAP服务器的连接,但未成功。

谁能告诉我如何连接。

 bool validres= ValidateLDAPUser("LDAP://dp01.XXX.XXX", "LDAP_account", "LDAP_password");
 Response.Write(validres + " <p>Ad Auth fail</p>");
static bool ValidateLDAPUser(string ldapserver, string userId, string password)
{
    try
    {
        using (var ldapConnection = new LdapConnection(
                new LdapDirectoryIdentifier($"{ldapserver}")))
        {
            ldapConnection.AuthType = AuthType.Basic;
            ldapConnection.AutoBind = false;
            ldapConnection.Timeout = new TimeSpan(0, 0, 0, 15);
            
            var ldapUserId = $"uid={userId}";
            var credential = new NetworkCredential(ldapUserId, password);
            ldapConnection.Bind(credential);
            Console.WriteLine("Successfully authenticated to ldap server " + ldapserver);
            return true;
        }

    }
    catch (LdapException e)
    {
        Console.WriteLine(("Error with ldap server " + ldapserver + e.ToString()));
        return false;
    }
}

0 个答案:

没有答案