扩展的 UserPrincipal 只返回一个空字符串

Extended UserPrincipal Only Returning an Empty String

我试图找到一种方法来从默认情况下不在 UserPrincipal 对象中的活动目录用户获取某些属性。在搜索我的答案时,我遇到了 this question。我确定@marc_s 发布的答案是正确的,它至少是非常有用的。由于它的实用性,我创建了一个 UserPrincipalEx class 并完成了他的回答中建议的所有操作,如@marc_s。 class 本身似乎被正确定义,除了我的扩展属性这一事实,在本例中 title 总是 returning 一个空字符串。我在 AD 中再次检查该属性名为 title,所以我应该抓住正确的属性,但我没有得到任何内容。

@marc_s 还提到 class 是一个 骨架 但如果这就是我无法让它工作的原因,我还需要什么要添加?

我在他们自己的 .cs class 中定义了他的两个 class 称为 UserPrincipalEx.cs 我同时拥有 UserPrincipalExSearchFilterUserPrincipalEx classes 在该文件中定义。当我在我的代码中创建我的对象时,它总是 return 是一个空字符串。

UserPrincipalEx user = new UserPrincipalEx(principal.Context);
if (user == null) return false;
return user.Title == "A value other than empty" | (user.Title == "" || user.Title == null);

我还需要做什么才能从AD中获取title的值?

更新:

如果我按照 title 属性的检索进行操作,UserPrincipalEx 总是说它为空,这继续让我觉得我对 UserPrincipalEx.cs[=26= 做错了什么]

[DirectoryProperty("title")]
public string Title
{
    get
    {
        if (ExtensionGet("title").Length != 1)
            return string.Empty;

        return (string)ExtensionGet("title")[0];
    }
    set { ExtensionSet("title", value); }
}

它总是命中 return string.Empty; 并且就像行中所说的那样,returns 是一个空字符串

更新二:

据我所知,无论您要查找什么扩展属性,UserPrincipalEx 都将始终 return 为 null 并使字符串为空。

我没有使用扩展的 UserPrincipal,而是使用 DictionaryEntry 来查找值并像这样进行比较。

UserPrincipal user = new UserPrincipal(principal.Context);
if (user == null) return false;
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
string title = "";
if (directoryEntry.Properties.Contains("title"))
{
    title = directoryEntry.Properties["title"].Value.ToString();
}
return title == "Comparison Value" | (title == "" || title == null);