简体   繁体   中英

How can I set an Extended UserPrincipal Array Property as empty with System.Directory.AccountManagement

As mentioned in the title I'm using the Principal Extensions of the Account Management API for getting and setting custom AD User attributes. Two of these custom attributes are string array. The problem im encountering right now is that I get an error when ever I'm trying to set a property as an empty array.

My Extended UserPrincipal looks like this:

    [DirectoryRdnPrefix("CN")]
    [DirectoryObjectClass("user")]
    class UserPrincipalEx : UserPrincipal
    {
        public UserPrincipalEx(PrincipalContext context) : base(context) { }
        public UserPrincipalEx(PrincipalContext context, string samAccountName, string password, bool enabled):base(context, samAccountName, password, enabled) { }
        
        [DirectoryProperty("course")]
        public string[] course
        {
            get
            {
                int len = ExtensionGet("course").Length;

                string[] courses = new string[len];
                object[] coursesRaw = ExtensionGet("course");

                for (int i = 0; i < len; i++)
                {
                    courses[i] = (string)coursesRaw[i];
                }
                return courses;
            }
            sset 
            {
                this.ExtensionSet("course", value);
            }
        }

        [DirectoryProperty("interested")]
        public string[] interested
        {
            get
            {
                int len = ExtensionGet("interested").Length;

                string[] interested = new string[len];
                object[] interestedRaw = ExtensionGet("interested");

                for (int i = 0; i < len; i++)
                {
                    interested[i] = (string)interestedRaw[i];
                }
                return interested;
            }
            set 
            {
                this.ExtensionSet("interested", value);
            }
        }
        public static new UserPrincipalEx FindByIdentity(PrincipalContext context, string identityValue)
        {
            return (UserPrincipalEx)FindByIdentityWithType(context, typeof(UserPrincipalEx), identityValue);
        }

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

And I'm trying to set the properties like this:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Settings.Default.DCName))
{
       UserPrincipalEx exUser = UserPrincipalEx.FindByIdentity(pc, newMailUser.Name);
                        
       exUser.interested = CourseInterested.ToStringArray(newMailUser.CInterested);
       exUser.course = CourseVisited.ToStringArray(newMailUser.CVisited);
                        
       exUser.Save(pc);
}

The ToStringArray Methods return a string array with values or an empty one. The code fails when newMailUser.CInterested or newMailUser.CVIsited has no entries and an empty string array is returned with the error message "Collections whoes elements are another collection cannot be set by ExtensionClasses." and further the error says that it occured at the set methode of the extended property ExtensionSet("property", value)

I used this microsoft example. I also tried this approach but sadly it didn't work for me.

If anyone would be so kind to help I would appreciate it.

Well It was way easier then I thought. I just had to return null from my ToStringArray methods if there are no entries in newMailUser.CInterested or newMaliUser.CVisited because the ExtensionSet method has no problem with null .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM