【问题标题】:LDAP Connecting with IP address and Port NumberLDAP 与 IP 地址和端口号连接
【发布时间】:2017-09-27 23:05:11
【问题描述】:

我工作的公司有一个产品,它使用 Active Directory 来启用我们产品的安全功能,并使用包含 DirectoryEntryDirectorySearcher 组件的库。

如果某人是FOO 组的成员,则他们具有标准访问权限。如果他们是FOO-ADMIN 的成员,则他们拥有管理员权限。

我们有一个不使用 Active Directory 的潜在客户。他们有一个运行 LDAP 的 Apache 服务器,他们提供了这个屏幕截图。

上面,看来我需要连接到 xxx.xxx.5.101:389 的域(即 DirectoryEntry("LDAP://xxx.xxx.5.101:389")) ,但是“DN 或用户”字段如何与密码匹配?

Active Directory 组件是否能够在 Apache 系统上进行 LDAP 身份验证,或者代码是否需要完全不同的控件?

这是我整理的一些粗略代码:

/// <summary>
/// Untested Method
/// </summary>
/// <param name="hostIp">String (EX: xxx.xxx.5.101)</param>
/// <param name="port">Int (EX: 389)</param>
/// <param name="user">String (EX: cn=danibla,ou=sysdata,ou=townhall,o=toh)</param>
/// <param name="password">String - provided password</param>
/// <param name="groupsLike">String (EX: find all groups like FOO)</param>
/// <returns>String[] array of matching membership groups</returns>
public static String[] GetMemberships(String hostIp, int port, String user, String password, String groupsLike)
{
    var results = new List<String>();
    var path = String.Format("LDAP://{0}:{1}", hostIp, port);
    using (var entry = new DirectoryEntry(path, user, password))
    {
        using (var search = new DirectorySearcher(entry, String.Format("(CN={0}*)", groupsLike)))
        {
            var expression = new Regex("CN=([^,]*),", RegexOptions.Compiled & RegexOptions.IgnoreCase);
            foreach (SearchResult item in search.FindAll())
            {
                var match = expression.Match(item.Path);
                var name = match.Groups[1].Value;
                if (name.StartsWith(groupsLike, StringComparison.OrdinalIgnoreCase))
                {
                    if (!results.Contains(name))
                    {
                        results.Add(name);
                    }
                }
            }
        }
    }
    return results.ToArray();
}

我对他们为“DN 或用户”字段传递的“类似路径”参数感到困扰,尤其是当它显示他们提供密码时。

我们没有 Apache 环境来测试它。我们公司不希望我带着很多不必要的问题去找这个客户。

更新:
仍然需要一种方法来做到这一点。开始赏金。也许对此引起一些关注会给我一个解决方案。

在上面的屏幕截图中,代码中username 的值既是cn-mikead,ou=sysdata,ou=townhall,o=toh,又是mikead,两者在调用FindAll() 时都有相同的COM Exception。

这是我现在的代码。

public static String[] Groups(String domain, int port, String username, int authenticationValue, String startsWith)
{
    String name;
    var results = new List<String>();
    var ldapPath =
        String.IsNullOrEmpty(domain) ? null :
        (0 < port) ?
        String.Format("LDAP://DC={0}:{1}", domain, port) :
        String.Format("LDAP://DC={0}", domain);
    using (var entry = new DirectoryEntry(String.Format("WinNT://{0}/{1}", Environment.UserDomainName, username)))
    {
        name = String.Format("{0}", entry.Properties["fullName"].Value);
    }
    var filter = String.Format("(CN={0}", name);
    var expression = new Regex("CN=([^,]*),", RegexOptions.Compiled & RegexOptions.IgnoreCase);
    using (var entry = new DirectoryEntry(ldapPath))
    {
        entry.AuthenticationType = (AuthenticationTypes)authenticationValue;
        using (var search = new DirectorySearcher(entry) { Filter = filter })
        {
            search.PropertiesToLoad.Add("memberOf");
            try
            {
                foreach (SearchResult item in search.FindAll())
                {
                    foreach (var property in item.Properties["memberOf"])
                    {
                        var name = expression.Match(String.Format("{0}", property)).Groups[1].Value;
                        if (name.StartsWith(startsWith, StringComparison.OrdinalIgnoreCase))
                        {
                            if (!results.Contains(name))
                            {
                                results.Add(name);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                LogError("Groups", err);
            }
        }
    }
    return results.ToArray();
}

【问题讨论】:

  • LDAP是一个协议,所以只要你使用的产品只使用协议的标准操作,连接Apache DS而不是AD应该没有问题。如果您使用不属于 LDAP 协议一部分的 AD 自定义控件,问题将会出现,但只有您知道;)

标签: c# active-directory ldap directoryentry directorysearcher


【解决方案1】:

Apache 可以运行 LDAP,我的建议是确保您的客户端在其服务器上正确配置了 LDAP。这可以在他们服务器上的 httpd.conf 中完成

【讨论】:

  • 这应该是我不好的评论
  • 向我发送屏幕截图的人是另一家公司的 IT 人员,他们能够连接到他们的 LDAP。我们需要一种方法让我们的程序使用他们的 LDAP 进行身份验证。如果我们不能,我们不进行销售。上面显示的是他在名为 eDir 的工具上用来连接的所有参数(我不知道那是什么)。
  • 如果他们使用 LDAP 进行身份验证,他们将拥有一个 LDAP 服务器配置,您将需要用户名、密码、服务器名和 LDAP 驱动程序。如果您按照我所说的那样使用 apache,则必须使用 httpd.conf 连接到他们的 LDAP 服务器配置
  • 你试过ping他们的IP和它所在的端口吗?
  • 好吧,我的建议是创建一个 apache 环境,如果你只是在谷歌搜索如何在 Windows 上设置 Apache24,假设你使用的是 Windows 并运行它,然后运行你的程序在 httpd.conf 中使用他们的 LDAP 配置,您应该能够正确测试它
【解决方案2】:

我希望我有更多的时间给你一个更完整的答案。但是让我看看这是否有帮助。组成员在 eDirectory 中的工作方式不同,并且没有 memberOf 属性。您还可能会发现您必须低于 DirectoryEntry、DirectorySearcher 等...(因为这些是为 AD 量身定制的)。 System.DirectoryServices.Protocols 将为您提供较低级别的访问权限。

另外,Novell 也有您可以考虑使用的 c# 库:https://www.novell.com/developer/ndk/ldap_libraries_for_c_sharp.html

  1. 建议你先绑定到数据库,以具有搜索权限的用户或匿名用户(如果匿名可以搜索),然后搜索 (&(cn=USERNAME)(objectclass=Person)) 找到dn 你需要绑定为。
  2. 现在使用提供的凭据绑定为您找到的用户 dn,并获取 groupMembership 属性。
  3. 检查 groupMembership 属性以确定您的权限。

如果您无法使 groupMembership 属性起作用,或者,您可以在目录中搜索组:((cn=GROUPNAME)(objectclass=groupOfNames)) 然后你可以查看 groupOfNames:member 属性来找到你的用户名。

我会首先尝试绑定/验证,然后添加组内容。这里有一个绑定的例子:https://www.codeproject.com/Articles/5969/Authentication-against-Active-Directory-and-Edirec

如果您有证书问题,请在此处使用另一种方法: https://www.codeproject.com/Articles/19097/eDirectory-Authentication-using-LdapConnection-and

这里有一些有用的参考资料:

https://www.mediawiki.org/wiki/Extension:LDAP_Authentication/Examples#Configuration_for_non-AD_domains

https://docs.oracle.com/cd/E36500_01/E36503/html/ldap-filters-attrs-users.html#ldap-filters-attrs-users-openldap

https://www.ibm.com/support/knowledgecenter/en/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/rwlp_config_edirectoryLdapFilterProperties.html

Connecting to LDAP from C# using DirectoryServices

https://forums.novell.com/showthread.php/491292-Is-user-member-of-group-in-C

https://www.novell.com/documentation/developer/ldapcsharp/?page=/documentation/developer/ldapcsharp/cnet/data/bovtz77.html

http://mikemstech.blogspot.com/2013/03/searching-non-microsoft-ldap.html

https://www.sqlservercentral.com/Forums/Topic811694-391-1.aspx

【讨论】:

  • System.DirectoryServices.Protocols 带我到一篇文章Introduction to System.DirectoryServices.Protocols。我现在正在读那个。这可能是我需要的!
  • 这并没有完全解决我的问题,但我也没有完全解决您在此处添加的链接列表。谢谢。
  • 非常感谢慷慨的赏金。如果您遇到特定问题,请告诉我,我可以尝试进行更多研究。我在 C# 中实现了 AD LDAP 集成,并在 Ruby 中实现了非 AD LDAP 实现。但是我没有在 c# 中做过非 AD,所以很遗憾我没有代码可以分享。
猜你喜欢
  • 1970-01-01
  • 2020-01-14
  • 2014-03-14
  • 1970-01-01
  • 2010-11-17
  • 1970-01-01
  • 2010-10-30
  • 2021-12-11
  • 1970-01-01
相关资源
最近更新 更多