【发布时间】: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.CInterested 或newMailUser.CVIsited 没有条目并且返回一个空字符串数组并显示错误消息“ExtensionClasses 无法设置其元素是另一个集合的集合”时,代码将失败。并且进一步的错误说它发生在扩展属性ExtensionSet("property", value)的设置方法中@
我使用了this microsoft 示例。我也尝试了this 方法,但遗憾的是它对我不起作用。
如果有人愿意提供帮助,我将不胜感激。
【问题讨论】:
标签: c# .net directoryservices