C# 创建具有帐户管理扩展类属性的AD用户对象时违反约束
我正在创建一个WCF服务来检索和更新/创建ADC# 创建具有帐户管理扩展类属性的AD用户对象时违反约束,c#,active-directory,account-management,C#,Active Directory,Account Management,我正在创建一个WCF服务来检索和更新/创建ADPerson对象,但遇到了一个障碍。我创建了一个扩展类来管理扩展属性(交付的模式属性,但不在默认帐户管理类属性集中)。检索或更新这些扩展属性并没有问题,但当我尝试在AD中创建一个新的person对象时,会收到一个约束冲突 System.DirectoryServices.DirectoryServicesCOMException:发生约束冲突 我目前正在Windows 8.1桌面上的Visio 2013中以调试模式进行测试。代码如下。非常感谢任何人提
Person
对象,但遇到了一个障碍。我创建了一个扩展类来管理扩展属性(交付的模式属性,但不在默认帐户管理类属性集中)。检索或更新这些扩展属性并没有问题,但当我尝试在AD中创建一个新的person对象时,会收到一个约束冲突
System.DirectoryServices.DirectoryServicesCOMException:发生约束冲突
我目前正在Windows 8.1桌面上的Visio 2013中以调试模式进行测试。代码如下。非常感谢任何人提供的任何提示或见解
希望下面的代码有足够的文档记录,并且有意义。提前谢谢
更新:我应该更清楚。我非常确定这是扩展属性的原因是,当我注释掉调用代码(现在在下面的代码部分中注释)中设置这些属性的那些行时,它将创建没有错误的对象
这是我的电话号码:
....other code.....
PrincipalContext pc = null;
try {
pc = new PrincipalContext(ContextType.Domain, MyProject.ADAccountService.Properties.Settings.Default.Domain, MyProject.ADAccountService.Properties.Settings.Default.PeopleDN, MyProject.ADAccountService.Properties.Settings.Default.AdminAcct, MyProject.ADAccountService.Properties.Settings.Default.AdminPW);
}
catch (Exception e) {
defaultLogger.Warn(MyProject.ADAccountService.App_GlobalResources.Messages.PrincipalContextCreateFail, e);
// Application.Exit();
}
....other code looking for whether ADObject already exists...
// Create the new UserPrincipal object
if (!newADPerson.personExists) {
using (ADeXt userNew = new ADeXt(pc)) {
string randomPassword = System.Web.Security.Membership.GeneratePassword(20, 4);
if (newADPerson.officePhone != null && newADPerson.officePhone.Length > 0) { userNew.VoiceTelephoneNumber = newADPerson.officePhone; }
if (newADPerson.department != null && newADPerson.department.Length > 0) { userNew.department = newADPerson.department; } //offending codeline
if (newADPerson.title != null && newADPerson.title.Length > 0) { userNew.title = newADPerson.title; } //offending codeline
if (newADPerson.faxNumber != null && newADPerson.faxNumber.Length > 0) { userNew.facsimileTelephoneNumber = newADPerson.faxNumber; } //offending codeline
if (newADPerson.officeLocation != null && newADPerson.officeLocation.Length > 0) { userNew.physicalDeliveryOfficeName = newADPerson.officeLocation; } //offending codeline
if (newADPerson.isEmployee) {
//if an employee and (newADPerson.script == null) use default value from global project settings
userNew.ScriptPath = newADPerson.script ?? MyProject.ADAccountService.Properties.Settings.Default.defaultScript;
}
if (newADPerson.lastName != null && newADPerson.lastName.Length > 0) { userNew.Surname = newADPerson.lastName; }
if (newADPerson.firstName != null && newADPerson.firstName.Length > 0) { userNew.GivenName = newADPerson.firstName; }
if (newADPerson.emplID != null) { userNew.EmployeeId = newADPerson.emplID; }
if (newADPerson.displayName != null && newADPerson.displayName.Length > 0) { userNew.DisplayName = newADPerson.displayName; }
userNew.SamAccountName = AccountID;
userNew.Name = AccountID;
userNew.UserPrincipalName = AccountID + MyProject.ADAccountService.Properties.Settings.Default.ExchangeAddress;
try {
userNew.Save();
userNew.SetPassword(randomPassword);
}
catch (Exception e) {
pc.Dispose();
}
}
}
扩展类代码:
namespace MyProject.ADAccountService.Classes {
[DirectoryObjectClass("user")]
[DirectoryRdnPrefix("CN")]
class ADeXt : UserPrincipal {
public ADeXt(PrincipalContext context)
: base(context) {
}
public ADeXt(
PrincipalContext context,
string Container, //new constructor parameter added resolving issue
string samAccountName,
string password,
bool enabled
)
: base(
context,
samAccountName,
password,
enabled
) {
}
public static new ADeXt FindByIdentity(PrincipalContext context, string identityValue) {
return (ADeXt)FindByIdentityWithType(context, typeof(ADeXt), identityValue);
}
[DirectoryProperty("physicalDeliveryOfficeName")]
public string physicalDeliveryOfficeName {
get {
object[] result = this.ExtensionGet("physicalDeliveryOfficeName");
if (result != null) {
return (string)result[0];
}
else {
return null;
}
}
set {
this.ExtensionSet("physicalDeliveryOfficeName", value);
}
}
[DirectoryProperty("department")]
public string department {
get {
object[] result = this.ExtensionGet("department");
if (result != null) {
return (string)result[0];
}
else {
return null;
}
}
set {
this.ExtensionSet("department", value);
}
}
[DirectoryProperty("title")]
public string title {
get {
object[] result = this.ExtensionGet("title");
if (result != null) {
return (string)result[0];
}
else {
return null;
}
}
set {
this.ExtensionSet("title", value);
}
}
[DirectoryProperty("facsimileTelephoneNumber")]
public string facsimileTelephoneNumber {
get {
object[] result = this.ExtensionGet("facsimileTelephoneNumber");
if (result != null) {
return (string)result[0];
}
else {
return null;
}
}
set {
this.ExtensionSet("facsimileTelephoneNumber", value);
}
}
}
}
谢谢Marc,这个提示帮助我解决了这个问题。在扩展构造函数中为容器添加了新参数,这就成功了 更改了扩展类中的构造函数以添加默认容器。新构造函数现在列出如下内容:
public ADeXt(
PrincipalContext context,
**string Container,**
string samAccountName,
string password,
bool enabled
)
: base(
context,
samAccountName,
password,
enabled
) {
}
如果我正确理解您的代码,如果您仅使用
PrincipalContext
对象作为参数调用AdExt
构造函数,则不会调用将设置强制属性(如SamAccountName
等)的基构造函数。因此,您必须了解Person
类的强制属性是什么,并确保始终从代码中设置这些属性!谢谢Marc,这个提示帮助我解决了这个问题。在扩展构造函数中为容器添加了新参数,这就成功了。