C# 通过UserPrincipal获取Active Directory ExtensionAttribute

C# 通过UserPrincipal获取Active Directory ExtensionAttribute,c#,active-directory,derived-class,C#,Active Directory,Derived Class,我正在尝试获取用户的扩展属性4的值 这是我对UserPrincipal的扩展类: [DirectoryRdnPrefix("CN")] [DirectoryObjectClass("Person")] public class UserPrincipalEx : UserPrincipal { // Implement the constructor using the base class constructor. public UserPrincipalEx(Princip

我正在尝试获取用户的
扩展属性4
的值

这是我对
UserPrincipal
的扩展类:

[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
    // Implement the constructor using the base class constructor. 
    public UserPrincipalEx(PrincipalContext context) : base(context)
    { }

    // Implement the constructor with initialization parameters.    
    public UserPrincipalEx(PrincipalContext context,
                         string samAccountName,
                         string password,
                         bool enabled) : base(context, samAccountName, password, enabled)
    { }

    // Create the "extensionAttribute4" property.    
    [DirectoryProperty("extensionAttribute4")]
    public string ExtensionAttribute4
    {
        get
        {
            if (ExtensionGet("extensionAttribute4").Length != 1)
                return string.Empty;

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

    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
    }

    // Implement the overloaded search method FindByIdentity. 
    public static new UserPrincipalEx FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
    {
        return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityType, identityValue);
    }
}
下面是我如何调用该方法的:

 UserPrincipalEx user = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");
我在debug中看到,用户实际上是
Classes.UserPrincipalEx.FindByIdentity
的结果,但是
extensionAttribute4
没有列出。完全还有其他属性

我试着和经理做同样的事情,同样的结果。错误是:

对象引用未设置为对象的实例

我是新来的,所以如果我遗漏了一些明显的东西,请原谅

表示如果不存在具有该名称的属性,则返回“
null
”。如果属性为空,则认为它不存在,并返回
null
。所以你必须检查一下

[DirectoryProperty("extensionAttribute4")]
public string ExtensionAttribute4
{
    get
    {
        var extensionAttribute4 = ExtensionGet("extensionAttribute4");
        if (extensionAttribute4 == null || extensionAttribute4.Length != 1)
            return string.Empty;

        return (string)extensionAttribute4[0];
    }
    set { ExtensionSet("extensionAttribute4", value); }
}
请注意,通过使用和使用底层的
DirectoryEntry
对象,您可以在不扩展
UserPrincipal
的情况下执行相同的操作:

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");
var extensionAttribute4 = ((DirectoryEntry) user.GetUnderlyingObject())
                            .Properties["extensionAttribute4"]?.Value as string;

完美的谢谢我只是不明白为什么它不返回整个广告对象。在PSH中,您只需编写get-aduser username-properties*,就是这样……是的,
UserPrincipal
类并不公开AD中存在的所有属性(即使它实际上在后台加载它们)。这就是我不再使用它的原因之一。我只是直接使用
DirectoryEntry
。不管怎样,它通常会更快。谢谢,我下次会试试,这个“层次”太可笑了。非常感谢,真的
DirectoryEntry
本身是本机窗口上的一个层。