无法使用C#和LdapConnection对Apache DS进行身份验证? 问题

无法使用C#和LdapConnection对Apache DS进行身份验证? 问题,c#,ldap,openldap,apacheds,C#,Ldap,Openldap,Apacheds,我安装并配置了一个运行ldap的ApacheDS服务器。这对我自学ldap来说是一个巨大的进步。但是,以下C#控制台代码返回以下错误: System.DirectoryServices.Protocols.LdapException {"The supplied credential is invalid"} 我的代码是使用此示例代码对示例用户进行身份验证 代码 Program.cs using System; using System.Collections.Generic; using S

我安装并配置了一个运行ldap的ApacheDS服务器。这对我自学ldap来说是一个巨大的进步。但是,以下C#控制台代码返回以下错误:

System.DirectoryServices.Protocols.LdapException {"The supplied credential is invalid"}
我的代码是使用此示例代码对示例用户进行身份验证

代码 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SampleLdapAuthentication
{
    class Program
    {
        static void Main(string[] args)
        {
            RunLdap run = new RunLdap("localhost", "organization", 635, "hderp", "spaceballs1234");
            bool result = run.ValidateCredentials();
            if(result)
            {
                Console.WriteLine("Authentication Succeeded");
            }
            else
            {
                Console.WriteLine("Authentication Failed");
            }
        }
    }
}
sampleldapaauthentication.cs

using System;
using System.Collections.Generic;
using System.DirectoryServices.Protocols;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace SampleLdapAuthentication
{
    public class RunLdap
    {

        private static string _domainController;
        private static string _domain;
        private static int _port;
        private static string _userName;
        private static string _userPassword;



        //Constructor. Takes the domain controller, domain, port, username, and password and then calls Ldap Method to run authentication 
        public  RunLdap(string domainController, string domain, int port, string userName, string userPassword)
        {
            _domainController = domainController;
            _domain = null;
            _port = port;
            _userName = userName;
            _userPassword = userPassword;
        }



        public bool ValidateCredentials()
        {


            LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier(_domainController, _port);
            NetworkCredential networkCredential = new NetworkCredential(_userName, _userPassword, _domain);

            try
            {
                //We use using so we dispose the object as soon as it goes out of scope 
                using (LdapConnection connection = new LdapConnection(ldi))
                {

                    //connection.SessionOptions.SecureSocketLayer = true;
                    connection.AuthType = AuthType.Kerberos;
                    connection.Bind(networkCredential);

                    //Not sure what this is doing 


                }
                return true;

            }
            catch(LdapException ldapException)
            {
                return false;
            }


                return false;



        }//End of ValidateCredentials

    }
}
LDAP服务器详细信息

笔记 以下几点值得注意:

  • 我按照本教程创建了服务器和服务器
  • 据我所知,ApacheDS现在支持开箱即用的keberos,所以我的身份验证类型应该可以。也就是说,
    AuthType
  • 它在connection.Bind()方法上失败

我在想,我输入凭证的方式可能有问题,我的C#代码没有问题。这就是我包含服务器广告信息的原因。我不熟悉LDAP并使用它对用户进行身份验证,因此感谢您的帮助

您没有使用用户的可分辨名称。创建
NetworkCredential
对象时,应使用用户的区分名称,在这种情况下,
cn=Herp Derp,ou=users,o=organization
,而不是
hderp
。LDAP不知道在没有
o
ou
值的情况下从何处查找
hderp

这样做合法吗?也就是说,NetworkCredential NetworkCredential=新的NetworkCredential(\u cn,\u ou,\u o)?我将尝试在msdn上进行验证,但这似乎就是其中的含义。拜尔,我已经尝试使用
新的网络凭据(“cn=testUser,ou=water,o=toh,”p@ssword“
其中获取的基本DN=“ou=water,o=toh”…但每当我调用
ldap\u connection.Bind(credential)
时,仍然会得到“提供的凭据无效”。我还可以尝试什么?我正在尝试做同样的事情,但是我们的Apache服务器属于一个客户,我只能远程进入进行测试。请告诉我你能解决这个问题!你能告诉我你是怎么解决的吗?