是否可以使用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;
}在尝试实例化我正在使用的主体上下文时
PrincipalContext context = new PrincipalContext(
ContextType.Domain,
ldapServer,
null,
useSsl ? ContextOptions.SecureSocketLayer | ContextOptions.SimpleBind : ContextOptions.SimpleBind,
username,
password);为了完整起见,我在中传递了相同的值,用户名采用domain\user格式,而ldapServer采用server.domain.com格式,并在创建主体上下文时附加了带有:636的ldapServer。
我正在连接的服务器有证书问题,我猜这可能是因为LdapConnection设置为返回true进行验证而阻止了这一点。这不是一个问题,因为它是可信的。我没有访问服务器的权限,因此无法更改此设置。
发布于 2012-02-03 12:21:21
据我所知,当你以一个域为目标时,容器参数不能为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);发布于 2012-02-05 06:10:46
我怀疑您使用LDAP代码的部分问题是因为您使用的是AuthType.Basic。请尝试使用Negotiate。
https://stackoverflow.com/questions/9121854
复制相似问题