如何将 Active Directory pwdLastSet 转换为日期/时间

How to convert Active Directory pwdLastSet to Date/Time(如何将 Active Directory pwdLastSet 转换为日期/时间)
本文介绍了如何将 Active Directory pwdLastSet 转换为日期/时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

    public static string GetProperty(SearchResult searchResult, string PropertyName)
    {
        if (searchResult.Properties.Contains(PropertyName))
        {
            return searchResult.Properties[PropertyName][0].ToString();
        }
        else
        {
            return string.Empty;
        }
    }

上述方法适用于大多数 Active Directory 属性,但与日期/时间相关的属性除外,例如 pwdLastSet、maxPwdAge 等.

The above method works great for most Active Directory properties except those that are related to date/time such as pwdLastSet, maxPwdAge, etc.

我的问题是如何将 pwdLastSet 设置为人类可读的日期时间(例如 8/13/2013 或 2013 年 8 月 13 日等)

My question is how to I get the pwdLastSet to a human readable datetime (like 8/13/2013 or August 13, 2013, etc)

我试过了,但它抛出了异常

I've tries this but it threw exceptions

public static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
{
    var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
    var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
    return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
}

我使用以下代码获取时间为 Int64

I am using the following code to get the time as an Int64

Int64 passwordLastSet = ConvertADSLargeIntegerToInt64(objResult.Properties["pwdLastSet"][0]);

然后我打算使用 DateTime(Int64) 构造函数来创建一个 DateTime

Then I plan on using the DateTime(Int64) constructor to create a DateTime

推荐答案

根据 MSDN 文档:

此值存储为一个大整数,表示自 1601 年 1 月 1 日 (UTC) 以来的 100 纳秒间隔数.

This value is stored as a large integer that represents the number of 100 nanosecond intervals since January 1, 1601 (UTC).

这与 DateTime.FromFileTimeUtc、如所述完全一致这里.

而且我不确定为什么您觉得需要对整数进行低级操作.我想你可以施展它.

And I'm not sure why you feel the need to do the low level manipulation of the integer. I think you could just cast it.

所以就这样做:

long value = (long)objResult.Properties["pwdLastSet"][0];
DateTime pwdLastSet = DateTime.FromFileTimeUtc(value);

这篇关于如何将 Active Directory pwdLastSet 转换为日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to determine the type (AD User vs. AD Group) of an account?(如何确定帐户的类型(AD 用户与 AD 组)?)
How to resolve quot;The server does not support the control. The control is critical.quot; Active Directory error(如何解决“服务器不支持控件.控制至关重要.活动目录错误)
How to authenticate users with a customer#39;s (remote) active directory server(如何使用客户的(远程)活动目录服务器对用户进行身份验证)
How to know if my DirectoryEntry is really connected to my LDAP directory?(如何知道我的 DirectoryEntry 是否真的连接到我的 LDAP 目录?)
Add member to AD group from a trusted domain(将成员从受信任的域添加到 AD 组)
How to retrieve Users in a Group, including primary group users(如何检索组中的用户,包括主要组用户)