LDAP连接错误

本文关键字:错误 连接 LDAP | 更新日期: 2023-09-27 18:07:33

我的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();

下面是异常Error和堆栈跟踪:

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: 

任何想法?

LDAP连接错误

尝试如下:

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

你必须指定一些属性来加载findone()方法。在这个示例中,尝试查找用户的属性(username是一个字符串变量)。

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();

似乎您在DirectoryEntry的构造函数中定义了不同的路径,然后通过设置Path属性来覆盖它。如果您的服务器与RDN中的域名不同,则应在路径中定义它。你能试着用这种方法做吗,看看你会不会得到不同的错误?

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

并跳过通过属性设置路径的部分。

编辑:注意似乎你还错过了dc=za上的"d" .