C#:无法使用DirectoryEntry获取所有属性名称

时间:2016-08-26 07:10:33

标签: c# directoryentry

我有这段代码:

DirectoryEntry entry = new DirectoryEntry(_path + domain, domainAndusernam, password);
        try
        {
            Object obj = entry.NativeObject;
            string department, title;

            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(SAMAccountName=" + username + ")";

            search.PropertiesToLoad.Add("cn");
            search.PropertiesToLoad.Add("department");
            search.PropertiesToLoad.Add("title");
            SearchResult result = search.FindOne();

            if (result == null)
            {
                return false;
            }
            _path = result.Path;
            _filterAttribute = (string)result.Properties["cn"][0];
            department = (string)result.Properties["department"][0];
            title = (string)result.Properties["title"][0];
            return true;
        }
        catch (Exception)
        {
            return false;
        }

我的问题是,我似乎无法从SearchResult获取所有属性名称。我能够获得“cn”和“mail”,但不能获得“部门”和“标题”等其他属性。这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

如果某个属性在AD中没有为其分配,则该属性不会出现在结果集中。这是AD代码中的正常预期行为 - 如果您无法找到属性,则表示对象在AD中没有该属性的值。

所以如果"部门"对于尚未填写的特定用户,您不会获得一个空字符串作为"部门"价值 - "部门"而不会出现在result.Properties集合

相关问题