使用帐户管理扩展类属性创建 AD 用户对象时出现约束冲突

本文关键字:对象 用户 冲突 约束 AD 创建 管理 扩展 属性 | 更新日期: 2023-09-27 18:30:22

我正在创建一个 WCF 服务来检索和更新/创建 AD 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);
            }
        }
    }
}    

使用帐户管理扩展类属性创建 AD 用户对象时出现约束冲突

谢谢马克,这个提示帮助我解决了。 在扩展构造函数中为容器添加了新参数,这就可以了。

更改了扩展类中的构造函数以添加默认容器。 新的构造函数现在列出如下:

public ADeXt(
    PrincipalContext context,
    **string Container,**
    string samAccountName,
    string password,
    bool enabled
    )
    : base(
       context,
       samAccountName,
       password,
       enabled
       ) {
}

对于任何查看此错误的人。这可能意味着很多事情,第一个Google结果将显示它与PDC模拟器或复制有关。

就我而言,这是由于 employeeID 的字符太多(最多 16 个)。有时它是首字母缩写(6最大)或samAccountName(19最大值)。只需剥离田地,直到它作为起点。