我们的每个用户都根据他们所在的全局办事处分配到一个主要组织单位 (OU)。因此,“芝加哥”OU 包含我们芝加哥办事处的所有员工。
使用 c# 和 .net 3.5,我的任务是提取所有这些用户。
除非用户在卫星办公室或家庭办公室,否则他们的街道地址、城市、州等都是空的,但 OU 包含这些详细信息。在 Windows 的 Active Directory 界面中,右键单击 OU 并选择属性提供了一个放置所有这些信息的地方,就像在用户身上一样。但是,当我尝试像访问用户一样访问这些属性时,出现对象引用错误,提示这些属性对于 OU 的存在方式与它们对于用户的存在方式不同。
我如何/可以从 OU 对象访问这些位置参数?
这是我正在使用的代码示例,以 streetaddress 为例,尝试从 OU 分配 streetaddress 值的语句失败,而 associate 的分配成功。
foreach (SearchResult subOU in results)
{
ResultPropertyValueCollection subColl = subOU.Properties["distinguishedname"];
string subPath = subColl[0].ToString();
DirectoryEntry subEntry = new DirectoryEntry("LDAP://" + subPath);
DirectorySearcher userSearcher = new DirectorySearcher(subEntry);
userSearcher.SearchScope = SearchScope.OneLevel;
userSearcher.Filter = "(objectClass=user)";
foreach (SearchResult user in userSearcher.FindAll())
{
ResultPropertyValueCollection userColl = user.Properties["distinguishedname"];
string userPath = userColl[0].ToString();
DirectoryEntry userEntry = new DirectoryEntry("LDAP://" + userPath);
PropertyCollection associateProperties = userEntry.Properties;
PropertyCollection ouProperties = subEntry.Properties;
string streetAddress = string.Empty;
if (associateProperties["streetaddress"].Value == null)
{ streetAddress = ouProperties["streetaddress"].Value.ToString(); }
else
{ streetAddress = associateProperties["streetaddress"].Value.ToString(); }
}
}
请您参考如下方法:
如果您在 Active Directory 用户和计算机的常规选项卡上为用户更改街道字段,该值将存储在目录的街道地址属性中。但是,如果您更改某个 OU 的相同字段,则该值存储在目录中该 OU 的街道属性中。
这是因为 OU 对象不允许(如 Active Directory 默认架构中所定义)包含 streetAddress 属性。
因此(没有进一步分析您的代码)如果您将 ouProperties["streetaddress"] 更改为 ouProperties["street"] 您可能会得到如下结果正在寻找。
