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

时间:2015-03-08 18:47:55

标签: 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 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");
            }
        }
    }
}

SampleLdapAuthentication.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服务器详细信息

Attribute Description

Gui Tree

enter image description here

备注

以下是值得注意的事情:

  • 我在创建服务器和DIT时遵循了本教程。
  • 根据我的理解,ApacheDS现在支持开箱即用的keberos,所以我的身份验证类型应该没问题。也就是AuthType
  • 在connection.Bind()方法
  • 上失败

我想也许我输入凭据并且我的C#代码没问题。这就是我包含服务器AD信息的原因。我是LDAP的新手并使用它来验证用户身份,所以感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您没有使用用户的专有名称。在创建NetworkCredential对象时,您应该使用用户的distingushed名称,在这种情况下,cn=Herp Derp,ou=users,o=organization而不是hderp。在没有hderpo值的情况下,LDAP无法知道查找ou的位置。