无法为 AD 自定义属性分配大值

Unable to assign large value to AD custom attribute

我正在尝试为用户帐户的自定义 Active Directory 属性 msExchRecipientTypeDetails 分配一个值,如下所示,但它导致出现“未指定错误”COM 异常。我没有看到其他自定义属性(包括与 MS Exchange 相关的其他属性)出现此问题,也没有看到分配右侧的小值(例如 1)出现此问题。它似乎与安全无关。我该如何解决?

using (DirectoryEntry userEntry = result.GetDirectoryEntry())
{
    userEntry.Properties["msExchRecipientTypeDetails"].Value = 2147483648;
    userEntry.CommitChanges();
}

msExchRecipientTypeDetails属性是IADsLargeInteger类型,这是AD存储long(64位)整数的方式。在赋值前必须将数字转换为IADsLargeInteger

这会进行转换:

using ActiveDs;

private IADsLargeInteger LongToAdLong(long inputValue)
{
    IADsLargeInteger outputValue = new LargeInteger();     // 64-bit
    outputValue.HighPart = (int)(inputValue >> 32);        // 32-bit
    outputValue.LowPart = (int)(inputValue & 0xFFFFFFFF);  // 32-bit

    return outputValue;
}

使用它,您现在可以将值分配给属性:

using (DirectoryEntry userEntry = result.GetDirectoryEntry())
{
    var mailboxTypeValue = LongToAdLong(2147483648);
    userEntry.Properties["msExchRecipientTypeDetails"].Value = mailboxTypeValue;
    userEntry.CommitChanges();
}

顺便说一句,如果您直接将值分配给 AD Exchange 属性作为为新用户配置邮箱的过程的一部分,不要这样做,因为它不受支持并会导致电子邮件问题,其中一些问题可能并不明显。更好的方法是使用 PowerShell API 调用适当的 PowerShell 命令,例如 Enable-RemoteMailbox.