[英]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.