Как упоминалось в заголовке, я использую основные расширения API управления учетными записями для получения и настройки пользовательских атрибутов пользователя 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.CInterested или newMailUser.CVIsited не имеет записей и возвращается пустой массив строк с сообщением об ошибке. Коллекции, элементы которых являются другой коллекцией, не могут быть установлены с помощью ExtensionClasses. и далее ошибка говорит о том, что она произошла при задании метода расширенного свойства ExtensionSet("property", value)
Я использовал это пример майкрософт. Я также пробовал это подход, но, к сожалению, это не сработало для меня.
Если бы кто-нибудь был бы так любезен, чтобы помочь, я был бы признателен.