【问题标题】:Problem handling custom Active Directory date attribute处理自定义 Active Directory 日期属性的问题
【发布时间】:2023-07-14 06:44:01
【问题描述】:

我已经扩展了我们的模式,添加了一个大整数/间隔类型的新属性,用作日期。这里有问题。一是在ADUC 中,属性显示为数字,而系统中的其他日期(具有相同的基础类型)显示为日期。实际上,这似乎没问题,因为它是整数。也许ADUC 已经预设了一些属性,它强制显示为日期?

另一个是我无法使用System.DirectoryServices.Accountmanagment 以正确的方式处理此问题。我有一个扩展 UserPrincipal 的类,它工作正常,但添加这个新属性将不起作用。当我尝试传递日期以及长时间传递时,它都会失败。

我觉得我在 AD 架构过程中做错了,尽管我不确定它可能是什么。我试过用谷歌搜索,但到目前为止,我还没有在任何文章中发现任何类似的问题。

我很害怕做一些会弄乱我的AD 的事情,所以我在这里寻求一些关于此的提示,尤其是关于日期的提示。

编辑:

我进行了更多测试,并为 AccountManagment.UserPrincipal 扩展提供了解决方案。虽然它“感觉”很奇怪:

    public DateTime? MyCustomDate
    {
        get
        {

            object[] result = this.ExtensionGet("my-custom-date");
            if (result != null && result.Length > 0)
            {
                if (result[0].GetType() == typeof(string))
                {
                    long l = 0;
                    return (long.TryParse(result[0].ToString(), out l) ? DateTime.FromFileTimeUtc(l) : (DateTime?)null);
                }
                else
                {
                    ActiveDs.IADsLargeInteger li = (ActiveDs.IADsLargeInteger)result[0];
                    return DateTime.FromFileTimeUtc(((long)li.HighPart << 32) + li.LowPart).ToLocalTime();
                }
            }
            else
                return null;
        }
        set
        {
            if (value != null)
                ExtensionSet("my-custom-date", ((DateTime)value).ToFileTimeUtc().ToString());
            else
                ExtensionSet("my-custom-date", null);
        }
    }

我尝试了不同的方法,但似乎只有 ToString() 方法有效。

所以我基本上得到了它的工作。此外,我尝试扩展 accountExires 的 allready 内置属性。我得到了完全相同的行为,所以我相信我在 AD 中的属性是正确创建的。

编辑#2: 属性的 getter 部分中的类型检查用于当属性值位于内存中时,如果它已被 setter 自身的属性更改。该值不再是 com 类型,而是字符串(显然)

【问题讨论】:

    标签: c# active-directory ldap schema directoryservices


    【解决方案1】:

    由于我今天一直在处理这个问题,看来我在编辑中的解决方案可以完成这项工作。

    公共日期时间?我的自定义日期 { 得到 {

            object[] result = this.ExtensionGet("my-custom-date");
            if (result != null && result.Length > 0)
            {
                if (result[0].GetType() == typeof(string))
                {
                    long l = 0;
                    return (long.TryParse(result[0].ToString(), out l) ? DateTime.FromFileTimeUtc(l) : (DateTime?)null);
                }
                else
                {
                    ActiveDs.IADsLargeInteger li = (ActiveDs.IADsLargeInteger)result[0];
                    return DateTime.FromFileTimeUtc(((long)li.HighPart << 32) + li.LowPart).ToLocalTime();
                }
            }
            else
                return null;
        }
        set
        {
            if (value != null)
                ExtensionSet("my-custom-date", ((DateTime)value).ToFileTimeUtc().ToString());
            else
                ExtensionSet("my-custom-date", null);
        }
    }
    

    【讨论】: