2

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

如果某人是 group 的成员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的,在调用时都具有相同的 COM 异常FindAll()

这是我现在拥有的代码。

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();
}
4

2 回答 2

2

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

于 2017-10-05T20:07:40.170 回答
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

使用 DirectoryServices 从 C# 连接到 LDAP

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

于 2017-10-17T08:05:55.510 回答