using System.DirectoryServices.Protocols;
//LDAP域名
var host = "xx.xx.xx.xx.com:636";
//登录节点
var baseDN = "cn=xx,DC=xx";
//用户名
var adminName = "uid=xx,cn=xx,DC=xx";
//用户密码
var adminPass = "xx";
//根据上方信息新建连接
var conn = new LdapConnection(new LdapDirectoryIdentifier(host), new NetworkCredential(adminName, adminPass));
//使用ssl
conn.SessionOptions.SecureSocketLayer = true;
//规避证书验证
conn.SessionOptions.VerifyServerCertificate += delegate { return true; };
//配置连接认证方式
conn.AuthType = AuthType.Basic;
//无用户名及密码时使用匿名认证方式
conn.AuthType = AuthType.Anonymous;
try
{
//连接
conn.Bind();
//释放连接
conn.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("异常信息:" + ex.Message + "\n异常对象:" + ex.Source + "\n调用堆栈:" + ex.StackTrace.Trim() + "\n触发方法:" + ex.TargetSite);
}