【问题标题】:Unable to get certain fields form LDAP users无法从 LDAP 用户获取某些字段
【发布时间】:2014-09-03 00:56:58
【问题描述】:

我无法从 PasswordNeverExpires 等用户对象中获取某些字段。现在我正在循环浏览超过 2000 个用户返回的每个属性,并且我的条件断点从未中断过一次,所以我知道它不会返回。如果我无条件中断,则此代码返回的属性数始终为 1。 我们的服务器是 Windows 2003 Server。我可以从 NetEnum 命令中获得我想要的所有信息。 我看到其他人声称他们可以做到这一点,但我看不出我的代码有什么不同。当我不提供任何要加载的属性时,它会抓取大约 30-37 个属性。我需要和使用其中的几个属性。

    public void FetchUsers(string domainId, Sql sql)
    {
        var entry = new DirectoryEntry("LDAP://" + DomainControllerAddress, DomainPrefixedUsername, Password,
            AuthenticationType);

        var dSearch = new DirectorySearcher(entry)
        {
            Filter = "(&(objectClass=user)(!(objectclass=computer)))",
            SearchScope = SearchScope.Subtree,
            PageSize = 1000,

        };

        dSearch.PropertiesToLoad.Add("passwordneverexpires");

        var users = dSearch.FindAll();

        foreach (SearchResult ldapUser in users)
        {
            SaveUser(ldapUser, sql, domainId);
        }
    }

    private void SaveUser(SearchResult ldapUser, Sql sql, string domainId)
    {
        if (ldapUser.Properties.PropertyNames == null) return;

        foreach (string propertyName in ldapUser.Properties.PropertyNames)
        {
//I'm breaking here on the condition that propertyName != 'adspath' and it never breaks
            var v = ldapUser.Properties[propertyName];
        }

        return;
}

【问题讨论】:

    标签: c# active-directory ldap directorysearcher


    【解决方案1】:

    几件事:

    1. 您的基本过滤器效率很低。改用这个(&(objectCategory=person)(objectClass=user))
    2. 没有名为 passwordneverexpires 的属性。您需要检查用户的 userAccountControl 掩码中的第 13 位 - 请参阅 http://msdn.microsoft.com/en-us/library/aa772300%28v=vs.85%29.aspx 以获取值列表。
    3. 您永远不会闯入您的循环,因为您告诉客户只请求一个属性。

    【讨论】:

      【解决方案2】:

      您可以使用如下过滤器:(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=65536))to get All users with the account configuration DONT_EXPIRE_PASSWORD

      -吉姆

      【讨论】:

        猜你喜欢
        • 2021-10-20
        • 2020-08-30
        • 1970-01-01
        • 1970-01-01
        • 2017-05-09
        • 1970-01-01
        • 2021-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多