【问题标题】:Cannot authenticate against Apache DS using C# and LdapConnection?无法使用 C# 和 LdapConnection 对 Apache DS 进行身份验证?
【发布时间】:2015-05-09 22:14:42
【问题描述】:

问题

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

System.DirectoryServices.Protocols.LdapException {"The supplied credential is invalid"}

我的代码是使用此示例代码来验证示例用户。

代码

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

注意事项

以下是我在做的事情值得注意的地方:

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

我在想我输入凭据的方式可能有问题,我的 C# 代码很好。这就是我包含服务器 AD 信息的原因。我是 LDAP 新手,并使用它来验证用户身份,因此感谢您的帮助。

【问题讨论】:

  • 我正在尝试做同样的事情,但我们的 Apache 服务器属于一个客户,我只能远程进入该客户进行测试。请告诉我你能解决这个问题!你能更新一下你是如何解决的吗?

标签: c# ldap openldap apacheds


【解决方案1】:

您没有使用用户的专有名称。当您创建 NetworkCredential 对象时,您应该使用用户的专有名称,在这种情况下,cn=Herp Derp,ou=users,o=organization 而不是 hderp。如果没有 oou 值,LDAP 不知道在哪里寻找 hderp

【讨论】:

  • 这样的事情合法吗?即NetworkCredential networkCredential = new NetworkCredential(_cn, _ou, _o)?我会尝试在 msdn 上验证,但这似乎是暗示。
  • 先生。 Bair,我尝试与new NetworkCredentials("cn=testUser,ou=water,o=toh", "p@ssword") 连接,其中 Fetched Base DN = "ou=water,o=toh" ...但每当我调用ldap_connection.Bind(credential) 时,我仍然会收到“提供的凭据无效”。我还能尝试什么?
猜你喜欢
  • 2016-08-29
  • 1970-01-01
  • 2018-02-09
  • 2013-05-30
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多