【问题标题】:Constraint Violation When Creating AD User Object with account management extension class attributes使用帐户管理扩展类属性创建 AD 用户对象时违反约束
【发布时间】:2014-11-14 17:13:31
【问题描述】:

我正在创建 WCF 服务来检索和更新/创建 AD Person 对象,但遇到了障碍。我创建了一个扩展类来管理扩展属性(交付的架构属性,但不在默认帐户管理类属性集中)。检索或更新这些扩展属性没有问题,但是当我尝试在 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);
            }
        }
    }
}    

【问题讨论】:

  • 如果我正确理解了您的代码,如果您仅使用 PrincipalContext 对象作为参数调用 AdExt 构造函数,那么 base 构造函数将设置 强制属性像SamAccountName 等不会被调用。因此,您必须了解Person 类的强制属性是什么,并且确保您也始终从代码中设置这些属性!
  • 感谢 Marc,这个提示帮助我解决了问题。在扩展构造函数中为容器添加了新参数,这就成功了。

标签: c# active-directory account-management


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 1970-01-01
      • 2012-05-05
      • 2017-12-09
      • 2019-12-28
      • 2019-12-12
      • 2018-05-27
      • 2013-07-04
      • 1970-01-01
      相关资源
      最近更新 更多