簡體   English   中英

無法使用C#和LdapConnection對Apache DS進行身份驗證?

[英]Cannot authenticate against Apache DS using C# and LdapConnection?

問題

我安裝並配置了運行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服務器細節

屬性描述

桂樹

在此輸入圖像描述

筆記

我正在做的事情值得注意以下幾點:

  • 我在創建服務器和DIT時遵循了本教程。
  • 根據我的理解,ApacheDS現在支持開箱即用的keberos,所以我的身份驗證類型應該沒問題。 也就是說, AuthType
  • 它在connection.Bind()方法上失敗

我想也許我輸入憑證並且我的C#代碼沒問題。 這就是我包含服務器AD信息的原因。 我是LDAP的新手並使用它來驗證用戶身份,所以感謝您的幫助。

您沒有使用用戶的專有名稱。 當您創建NetworkCredential對象時,您應該使用用戶的distingushed名稱,在這種情況下, cn=Herp Derp,ou=users,o=organization而不是hderp 如果沒有oou值,LDAP不知道在哪里查找hderp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM