【问题标题】:Convert DateTime value to Integer8 for ActiveDirectory query将 DateTime 值转换为 Integer8 以进行 ActiveDirectory 查询
【发布时间】:2016-02-24 06:08:43
【问题描述】:

我正在尝试创建一个 ldap 查询来搜索 Active Directory 并过滤结果以仅返回 lastLogonTimestamp 字段的值超过 30 天的用户。

我正在寻找的 ldap 过滤器是这样的:

"(&(ObjectClass=User)(lastLogonTimestamp<=" + lastLogonTimeStampLimit + "))"

我的问题是我无法找到任何方法将 .net DateTime 值转换为 Active Directory 中 lastLogonTimestamp 字段的正确格式,我读到该字段是“Integer8”数据类型。

如果有帮助的话,我已经找到了另一种方式的转换:

DateTime.FromFileTime((long)(user.Properties["lastLogonTimestamp"][0]))

【问题讨论】:

  • 这里是您的问题的答案的链接stackoverflow.com/questions/7169749/… 此页面上接受的答案将解决问题
  • 如果有ToFileTime方法就好了……
  • @Damien_The_Unbeliever 谢谢,我真的应该发现这一点。我一定是过度依赖谷歌来提供答案!
  • 如果你愿意把你的 cmets 作为答案,我会打勾并给你积分。

标签: c# active-directory


【解决方案1】:

此代码用于将 AD 对象转换为有效的 DateTime。您可以将它用于 AD 中的任何日期值(此示例用于 lastLogon)。关键似乎是 ActiveDs 库。 long 演员表似乎不起作用,但 IADsLargeInteger 就好了!

这里是一个代码示例,包括您需要从 AD 类型转换为 DateTime 的所有内容:

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using ActiveDs; // Namespace added via ref to C:\Windows\System32\activeds.tlb

private DateTime? getLastLogin(DirectoryEntry de)
{
    Int64 lastLogonThisServer = new Int64();

    if (de.Properties.Contains("lastLogon"))
    {
        if (de.Properties["lastLogon"].Value != null)
        {
            try
            {
                IADsLargeInteger lgInt =
                (IADsLargeInteger) de.Properties["lastLogon"].Value;
                lastLogonThisServer = ((long)lgInt.HighPart << 32) + lgInt.LowPart;

                return DateTime.FromFileTime(lastLogonThisServer);
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多