【问题标题】:DirectoryEntry giving com exception when trying to connect ldap server using secure port尝试使用安全端口连接 ldap 服务器时,DirectoryEntry 给出 com 异常
【发布时间】:2019-01-23 01:10:24
【问题描述】:

异常快照 enter image description here 步骤 A=>验证正确的证书配置

我有一个窗口服务,我试图通过它从安全端口 636 (SSL) 连接 LDAP 服务器,所有证书都正确 已配置,我已经使用工具 ldap.exe 验证了这一点,还检查了 portqry 工具以检查端口 636 是否正在侦听 并且成功地做到了

STEP B=>代码片段不适用于安全端口 636(对于 SSL),但可在非安全端口 (389) 下正常工作 观察下面的代码在我基于控制台运行时运行良好 即使使用端口 636 的应用程序,但在作为窗口服务运行时也会失败。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.Protocols;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace SampleLDAPWindowsService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

            TestDirectoryEntryWay();

        }

        protected override void OnStop()
        {


        }

        }
        public DirectoryEntry createDirectoryEntry()
        {
            // create and return new LDAP connection with desired settings  
            DirectoryEntry ldapConnection = null;
            ldapConnection = new DirectoryEntry("LDAP://abc.domain.com:636", "DomainAdmin", "DomainAdmin123", AuthenticationTypes.SecureSocketsLayer);
            return ldapConnection;
        }

        public void TestDirectoryEntryWay()
        {
            DirectorySearcher _searcher = null;
            SearchResult result_user = null;
            DirectoryEntry de = createDirectoryEntry();
            try
            {
                object o = de.SchemaEntry;//Getting a com exception  as the SchemaEntry is null not sure why as the same is working properly in port 389 
                _searcher = new DirectorySearcher(de, "(&(objectClass=user)(SAMAccountName=" + "demouser1" + "))");
                if (_searcher != null)
                {
                    result_user = _searcher.FindOne();

                }
            }
            catch (Exception ex)
            {
                //Getting a com exception 

            }

        }
    }
}

STEP C=>在窗口服务的端口 636 和端口 389 中工作的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices.Protocols;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace SampleLDAPWindowsService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
            // TestDirectoryEntryWay();
            var isLogged2 = SignInLDAP2("DomainAdmin", "DomainAdmin123", ""LDAP://abc.domain.com:636"", "abc.domain.com", true);
        }

        protected override void OnStop()
        {


        }

        public  bool SignInLDAP2(string user, string psw, string ldapPath, string domain = null, bool useSSL = false)
        {
            // LdapConnection ldapConnection = new LdapConnection(ldapPath);

            var ldapDirectoryIdentifier = new LdapDirectoryIdentifier("abc.domain.com", 636, true, false);
            LdapConnection ldapConnection = new LdapConnection(ldapDirectoryIdentifier);

            if (useSSL)
            {
                ldapConnection.SessionOptions.SecureSocketLayer = true;

                ldapConnection.AuthType = AuthType.Negotiate;

                ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
            }

            //var networkCredential = new NetworkCredential("Hey", "There", "Guy");
            var networkCredential = new NetworkCredential(user, psw, domain);
            try
            {
                ldapConnection.Bind(networkCredential);

                bool exists = UserExists("demouser1");
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool UserExists(string username)
        {
            // create your domain context
            using (PrincipalContext domain = new PrincipalContext(ContextType.Domain, "abc.domain.com", "DomainAdmin", "DomainAdmin123"))
            {
                // find the user
                UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username);

                return foundUser != null;
            }
        }



        }
    }
}

问题在这里

在使用带有 DirectoryEntry 的安全端口时是否有问题,因为 LdapConnection 和 networkCredential 可以在两个端口(636 和 389)上顺利工作, 我有一个使用 DirectoryEntry 的旧代码,我希望它也适用于安全端口,有人可以帮助我,如何使 STEP B 为安全端口工作 也。

在此先感谢您的支持和指导。

【问题讨论】:

    标签: c# ssl directoryentry


    【解决方案1】:

    运行此证书的计算机可能不信任 SSL 证书。

    我使用 Chrome 来测试这个。像这样运行 Chrome(调整你的 Chrome 所在的路径):

    "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --explicitly-allowed-ports=636
    

    然后在 Chrome 中,转到 https://abc.domain.com:636。如果证书受信任,您将看到“无法连接”的消息。但如果它不受信任,Chrome 会给你一个大大的红色警告,你知道这就是问题所在。

    要信任证书,您需要获取根证书(作为文件,可能是 *.cer 或 *.crt)并将其安装在将运行您的代码的每台机器上。以下是在 Windows 中安装根证书的说明:https://www.thewindowsclub.com/manage-trusted-root-certificates-windows

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多