是否有可能使用來自System.DirectoryServices.Protocols的LdapConnection查詢Active Directory?使用LdapConnection枚舉AD
我有實例化一個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
與具有ldapServer格式:636創建主體上下文時追加。
我連接到該服務器有我的LdapConnection被設置爲驗證返回true猜測可能會阻止該證書的問題。這不是問題,因爲它是可信的。我無法訪問服務器,因此無法更改此設置。
你得到的錯誤是什麼。 – 2012-02-03 00:15:41
無法聯繫服務器。 – Sam 2012-02-03 00:18:42
嘗試LDAP服務器作爲'DC = MYDOMAIN,DC = com' – 2012-02-03 00:22:39