2012-02-07 33 views
2

我需要連接到可供我訪問的外部LDAP服務器,但只能通過LDAPS訪問。通過LDAPS檢索可訪問的外部Active Directory服務器上的所有用戶帳戶

我可用的信息是用戶名,服務器,密碼。我需要查詢和檢索所有用戶的列表。我在細節的格式是

  • 用戶名:域\用戶名
  • 密碼:{}密碼
  • 域:遠程{}域.net.au

下面的代碼我寫的會成功驗證我的用戶帳戶,但我現在需要枚舉所有用戶,這是我遇到的問題。理想情況下,這將是目錄中的所有用戶,而不是來自特定OU中的用戶。再次,我沒有完全合格的路徑到這個服務器的任何OU。服務器有一個自簽名證書,這就是爲什麼在我的例子中,我特意告訴它接受證書。

 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; 
+0

我discribe [有](http://stackoverflow.com/a/6457140/608772)三種使用C#訪問Active-Directory的方法。你真的需要使用System.DirectoryServices.Protocols嗎? – JPBlanc 2012-02-07 06:16:40

+0

@JPBlanc我不在乎,只要它能夠工作,我該如何去做。使用LdapConnection是我至今能夠使用此服務器運行LDAPS的唯一方式,我認爲這是由於證書錯誤造成的。如果它是標準的LDAP,那就沒問題了,因爲我有很多工作代碼直接與我的AD服務器通信。然而,這是另一個組織,我們只能通過LDAPS與之交談。 – Sam 2012-02-07 20:51:54

回答

2

因此,答案是介紹的Performing a Simple Search樣品中System.DirectoryServices.Protocols(S.DS.P)其中:

// create a search filter to find all objects 
string ldapSearchFilter = "(&(objectCategory=person)(objectClass=user))"; 
+0

非常感謝@JBBlanc。非常感激。 – Sam 2012-02-08 03:08:17

相關問題