1

我正在创建一个 WCF 服务来检索和更新/创建 ADPerson对象,但遇到了障碍。我创建了一个扩展类来管理扩展属性(交付的架构属性,但不在默认帐户管理类属性集中)。检索或更新这些扩展属性没有问题,但是当我尝试在 AD 中创建新的人员对象时,我收到约束违规

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);
            }
        }
    }
}    
4

2 回答 2

2

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

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

public ADeXt(
    PrincipalContext context,
    **string Container,**
    string samAccountName,
    string password,
    bool enabled
    )
    : base(
       context,
       samAccountName,
       password,
       enabled
       ) {
}
于 2014-11-14T18:41:07.683 回答
0

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

在我的情况下,这是由于employeeID 的字符太多(最多16 个)。有时它是首字母(最多 6 个)或 samAccountName(最多 19 个)。只需剥离字段,直到它作为起点。

于 2022-02-16T11:34:41.213 回答