2011-09-06 121 views
1

我有一個連接到我的LDAP的問題。它不斷給我一個COMExceptionError(參數不正確)LDAP連接錯誤

這裏是我的代碼至今:

static void Main(string[] args) 
    { 

     DirectoryEntry ldapConnection = new DirectoryEntry("10.9.130.113:667"); 
     ldapConnection.Path = "LDAP://ou=Users,ou=CorporateStore,ou=Absa,c=za"; 
     ldapConnection.AuthenticationType = AuthenticationTypes.Anonymous; 

     DirectorySearcher ds = new DirectorySearcher(ldapConnection); 
     SearchResult result = ds.FindOne(); 
     Console.ReadLine(); 
     if (result != null) 
     { 


      ResultPropertyCollection fields = result.Properties; 

      foreach (String ldapField in fields.PropertyNames) 
      { 


       foreach (Object myCollection in fields[ldapField]) 
        Console.WriteLine(String.Format("{0,-20} : {1}", 
            ldapField, myCollection.ToString())); 
       Console.ReadLine(); 
      } 

這是在發生錯誤的行:

SearchResult result = ds.findOne();

繼承人異常錯誤和堆棧跟蹤:

System.Runtime.InteropServices.COMException was unhandled 
    Message=The parameter is incorrect. 

    Source=System.DirectoryServices 
    ErrorCode=-2147024809 
    StackTrace: 
     at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
     at System.DirectoryServices.DirectoryEntry.Bind() 
     at System.DirectoryServices.DirectoryEntry.get_AdsObject() 
     at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) 
     at System.DirectoryServices.DirectorySearcher.FindOne() 
     at LDAPConnector.Program.Main(String[] args) in c:\documents and settings\expn261\my documents\visual studio 2010\Projects\LDAPConnector\LDAPConnector\Program.cs:line 23 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

任何想法?

+3

以及哪一行給出錯誤?你能包含異常細節和堆棧跟蹤嗎? –

+0

除了@Davide Piras的問題。這是AD還是另一臺LDAP服務器? – Vader

+0

是的,即時連接到遠程機器,這是一個ldap服務器。我正在使用的機器在同一個網絡/域上 – Trishen

回答

0

好像你在DirectoryEntry的構造函數中定義了不同的路徑,然後通過設置Path屬性來覆蓋它。如果你的服務器與RDN中的域不同,你應該在路徑中定義它。你能嘗試這樣做,看看你是否得到一個不同的錯誤?

DirectoryEntry ldapConnection = new DirectoryEntry("LDAP://10.9.130.113:667/ou=Users,ou=CorporateStore,ou=Absa,dc=za"); 

並跳過通過屬性設置路徑的部分。

編輯:通知它也似乎是你錯過了「d」的dc = za。

+0

c =是國家。有效的X.500。我懷疑你認爲這是AD作爲目標LDAP,我個人猜測是OpenLDAP或SunOne。 – geoffc

1

您必須指定一些要爲findone()方法工作的屬性。 在本示例中,嘗試查找用戶的屬性(用戶名是一個strig變量)。

DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain); //domain is a string with the FQDN (ex: int.domain.local) or alias (es: mydomainname) 

DomainControllerCollection dcc = DomainController.FindAll(context); 

DirectorySearcher ds; 
      ds = dcc[0].GetDirectorySearcher(); 
      ds.Filter = String.Format("(&(sAMAccountName={0})(objectClass=user))", username); 
      ds.PropertiesToLoad.Add("lastLogon"); 
      ds.PropertiesToLoad.Add("displayName"); 
      ds.PropertiesToLoad.Add("memberOf"); 
      ds.PropertiesToLoad.Add("userAccountControl"); 
      ds.PropertiesToLoad.Add("ADSPath"); 
      ds.PropertiesToLoad.Add("PrimaryGroupID"); 
      ds.PropertiesToLoad.Add("pwdLastSet"); 
      ds.PropertiesToLoad.Add("maxPwdAge"); 
      ds.PropertiesToLoad.Add("mail"); 
      ds.PropertiesToLoad.Add("distinguishedName"); 
      ds.PropertiesToLoad.Add("mdbstoragequota"); 
      ds.PropertiesToLoad.Add("SamAccountName"); 
      ds.SizeLimit = 15; 

      SearchResult sr = ds.FindOne(); 
+0

即使使用該代碼,它仍會在同一行中引發相同的異常。 – Trishen

+0

嘗試添加Directorycontext。我編輯了我的答案 –

+0

我的FQND外觀如下v121aplffd008.ds1.ad.absa.co.za。我現在得到一個「ActiveDirectoryOperationException」,指定的域名格式無效 – Trishen

1

嘗試以下操作:

  1. 如果LDAP服務器是AD,則必須在連接上進行綁定,因爲廣告不會允許匿名連接。
  2. 據我瞭解,您正在嘗試通過SSL進行連接,所以嘗試先不使用SSL進行連接(默認端口389),還嘗試按以下格式指定地址「ldaps://10.9.130.113:667」 。
  3. ldapConnection.Path
  4. 您不需要「LDAP://」前綴在使用搜索之前,嘗試執行簡單操作(如簡單綁定)以縮小問題範圍。