FilllanTroop
FilllanTroop

Reputation: 87

Get Active Directory ExtensionAttribute via UserPrincipal

I am trying to get the value of user's ExtensionAttribute4.

This is my extension class to 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);
    }
}

Here is how I call the method:

 UserPrincipalEx user = UserPrincipalEx.FindByIdentity(ctx, IdentityType.SamAccountName, "someuser");

I see in debug that user really is result of Classes.UserPrincipalEx.FindByIdentity, but extensionAttribute4 is not listed there. At all. Other attributes are there.

I tried to do the same with Manager, same result. The error is:

Object reference not set to an instance of an object

I am new to this, so please excuse me if I am missing something obvious.

Upvotes: 3

Views: 2623

Answers (1)

Gabriel Luci
Gabriel Luci

Reputation: 40988

The documentation for ExtensionGet() says that it returns "null if no attribute exists with that name". If the attribute is empty, it is considered as if it doesn't exist and it will return null. So you have to check for that.

[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); }
}

Note that you can do the same thing without extending UserPrincipal by using GetUnderlyingObject() and using the underlying DirectoryEntry object:

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

Upvotes: 3

Related Questions