LDAP连接错误

时间:2011-09-06 08:25:07

标签: c# ldap

我在连接到ldap时遇到问题。它一直给我一个COMExceptionError(参数不正确)

这是我到目前为止的代码:

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 :(得分: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();

答案 1 :(得分:1)

尝试以下方法:

  1. 如果您的LDAP服务器是AD,那么您必须在连接上执行绑定,因为AD不允许匿名连接。
  2. 据我所知,您正在尝试通过SSL连接,因此首先尝试连接不使用SSL(默认端口389),同时尝试使用以下格式指定地址“ldaps://10.9.130.113:667”
  3. ldapConnection.Path
  4. 中不需要“LDAP://”前缀
  5. 在使用搜索之前,尝试执行简单操作(如简单绑定)以缩小问题范围。

答案 2 :(得分:0)

似乎您在构造函数中为DirectoryEntry定义了不同的路径,然后通过设置Path属性来覆盖它。如果您的服务器与RDN中的域不同,则应在路径中定义它。您是否可以尝试以这种方式执行此操作并查看是否出现其他错误?

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

并跳过通过财产设置路径的部分。

编辑:通知您似乎错过了dc = za上的“d”。

相关问题