Skip to content

Principal ExtensionSet improper update for multi-valued properties with >2 values #34267

Description

@FWest98

I encountered a very strange issue when working with an extended Principal class that defined a multi-valued property from AD as follows:

[DirectoryProperty("multivalued")]
public string[] Data {
    get => ExtensionGet("multivalued").OfType<string>().ToArray();
    set => ExtensionSet("multivalued", value == null || value.Length == 0 ? null : value);
}

This seems to be the correct way, as far as I know it. In my testing, setting and unsetting these values works perfectly fine. However, updating such a value caused very inconsistent behaviour: removing a value from the list sometimes works, and sometimes it doesn't. The entity will simply not be updated. It turns out this never works when there are >1 elements remaining in the array.

When debugging this issue, I dug very deep down the rabbit hole and found the issue: it's in ADStoreCtx::ExtensionCacheToLdapConverter:

If the new value is an array of 1 element, the if-statement at line 1342 will fail, and the value will be set to the property at line 1389. This setter clears all property values first (here), and then sets the new value. Works perfectly.

However, if the new value is an array of more than 1 element, the if-statement at line 1342 will pass. The code after treats the value as a collection, and adds every element to the array in the target DirectoryEntry (line 1377). However, the current value of this property is never checked, and 'old' values are never removed. I would guess a de.Properties[kvp.Key].Clear(); before the foreach (line 1361) would fix the issue.

The current workaround I use is to first clear the property, save the entity, and then set the correct value. Not ideal, of course, but workable. If the current behaviour is intended, please update the documentation to reflect this; I couldn't find anything on this.

Edit: I guess this should be labeled as area-System.DirectoryServices

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions