2012-02-02 31 views
3

是否有可能使用來自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猜測可能會阻止該證書的問題。這不是問題,因爲它是可信的。我無法訪問服務器,因此無法更改此設置。

+0

你得到的錯誤是什麼。 – 2012-02-03 00:15:41

+0

無法聯繫服務器。 – Sam 2012-02-03 00:18:42

+0

嘗試LDAP服務器作爲'DC = MYDOMAIN,DC = com' – 2012-02-03 00:22:39

回答

2

至於我understant當您定位域容器參數不能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); 
+0

感謝您的反饋。在服務器上未啓用389(不安全的LDAP)時,最上面的一個將不起作用。它需要LDAPS。 我嘗試的第二個,都與server.domain.com和ldaps://server.domain.com。遠程LDAPS服務器的URI採用遠程。{域} .net.au的形式。我試過dc = {domain},dc = net,dc = au。你能確認什麼是正確的DC?感謝您的反饋意見。 – Sam 2012-02-05 22:24:15

+0

第三個參數是開始搜索的對象的區分名稱。我沒有iea的DN只是試圖啓動LDP.EXE(連接,綁定和查看樹)。對於我的DNS名稱domain.net.au DN應該是「dc = domain,dc = net,dc = au」 – JPBlanc 2012-02-06 04:22:12

0

我懷疑你的問題與LDAP代碼的部分是,你正在使用AuthType.Basic。改爲嘗試Negotiate

+0

感謝Brian,但這並沒有解決我的問題。 – Sam 2012-02-05 22:27:08