如何将Active Directory pwdLastSet转换为日期/时间 [英] How to convert Active Directory pwdLastSet to Date/Time
问题描述
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属性,除了
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文档:
此值存储为一个大整数,代表自1月1日以来的100纳秒间隔数,1601(UTC)。
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转换为日期/时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!