2011-09-06 115 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://”前缀在使用搜索之前,尝试执行简单操作(如简单绑定)以缩小问题范围。