【发布时间】:2015-04-26 19:07:33
【问题描述】:
我最初尝试将多个值作为 directReports 分配给 Active Directory 中的用户是使用 DirectoryEntry 对象并按如下方式分配:
DirectoryEntry de; //get it from somewhere
de.Properties["directReports"].Value = object[] { "CN=user123,CN=Users,DC=DOMAIN,DC=xyz", "dn2", "dn3" };
de.CommitChanges(); //error: contraint violation occurred
它也不适用于“经理”属性。
然后我开始使用 UserPrincipal 扩展方法(在后台使用 DirectoryEntries,对吗?)
[DirectoryRdnPrefix("CN")]
[DirectoryObjectClass("Person")]
public class UserPrincipalEx : UserPrincipal
{
public UserPrincipalEx(PrincipalContext pc)
: base(pc)
{
}
public void SetManager(string value)
{
this.ExtensionSet("manager", value);
}
public void SetDirectReports(string[] values)
{
//foreach(var v in values)
this.ExtensionSet("directReports", values);
}
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);
}
}
通过发送专有名称字符串分配经理可以正常工作,但不适用于直接下属。我仍然得到InvalidOperationException: A constraint violation occurred.
我试着这样称呼它:
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "mazen", user, pass);
UserPrincipalEx up = UserPrincipalEx.FindByIdentity(pc, IdentityType.SamAccountName, "moses");
var dns = new string[] { "CN=someone,CN=Users,DC=DOMAIN,DC=xyz", "CN=anotherone,CN=Users,DC=DOMAIN,DC=xyz" };
up.SetDirectReports(dns);
如何使用 C# 分配直接下属的多值属性?
【问题讨论】:
标签: c# active-directory