[英]Using LdapConnection to enumerate AD
是否可以使用System.DirectoryServices.Protocols中的LdapConnection来查询Active Directory?
我在实例化PrincipalContext时遇到问题。 这是我的代码,以防任何人发现问题:
private LdapConnection getLdapConnection(string username, string password, string ldapServer, bool secured)
{
int port = secured ? 636 : 389;
LdapConnection connection = new LdapConnection(new LdapDirectoryIdentifier(ldapServer, port, false, false));
if (secured)
{
connection.SessionOptions.ProtocolVersion = 3;
connection.SessionOptions.SecureSocketLayer = true;
}
connection.Credential = new NetworkCredential(username, password);
connection.AuthType = AuthType.Basic;
connection.SessionOptions.VerifyServerCertificate += (conn, cert) => { return true; };
connection.Bind();
return connection;
}
在尝试实例化我正在使用的Principal Context时
PrincipalContext context = new PrincipalContext(
ContextType.Domain,
ldapServer,
null,
useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
username,
password);
我传递相同的值,为了完整性,username的格式为
domain\\user\u003c/code> ,ldapServer的格式为
server.domain.com
,ldapServer在创建Principal Context时附加:636。
我连接的服务器有证书问题,我想这可能是因为LdapConnection被设置为返回true进行验证。 这不是问题,因为它是值得信赖的。 我无法访问服务器,因此无法更改此设置。
据我所知,当您定位域时,container参数不能为null
。 你可以尝试这个构造函数:
PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain,
"server.domain.com :389",
"dc=domain,dc=com",
username,
password);
然后这一个:
PrincipalContext domainContextMonou = new PrincipalContext(ContextType.Domain,
"server.domain.com :636",
"dc=domain,dc=com",
useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
username,
password);
我怀疑你在LDAP代码中遇到的部分问题是你使用的是AuthType.Basic
。 请尝试Negotiate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.