【问题标题】:How can I set an Extended UserPrincipal Array Property as empty with System.Directory.AccountManagement如何使用 System.Directory.AccountManagement 将扩展 UserPrincipal 数组属性设置为空
【发布时间】:2021-07-13 09:47:32
【问题描述】:

如标题中所述,我正在使用 Account Management API 的 Principal Extensions 来获取和设置自定义 AD 用户属性。其中两个自定义属性是字符串数组。我现在遇到的问题是,当我尝试将属性设置为空数组时出现错误。

我的扩展 UserPrincipal 如下所示:

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

我正在尝试这样设置属性:

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

ToStringArray 方法返回一个带值的字符串数组或一个空数组。当newMailUser.CInterestednewMailUser.CVIsited 没有条目并且返回一个空字符串数组并显示错误消息“ExtensionClasses 无法设置其元素是另一个集合的集合”时,代码将失败。并且进一步的错误说它发生在扩展属性ExtensionSet("property", value)的设置方法中@

我使用了this microsoft 示例。我也尝试了this 方法,但遗憾的是它对我不起作用。

如果有人愿意提供帮助,我将不胜感激。

【问题讨论】:

    标签: c# .net directoryservices


    【解决方案1】:

    嗯,这比我想象的要容易得多。如果newMailUser.CInterestednewMaliUser.CVisited 中没有条目,我只需要从我的ToStringArray 方法中返回null,因为ExtensionSet 方法与null 没有问题。

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      相关资源
      最近更新 更多