[英]Get AD user creation date in C#
我目前正在嘗試從廣告中獲取其他信息。 但我在拉動用戶創建日期時遇到了一些問題。
我在某些用戶上獲得了正確的日期,但在其他用戶上卻得到了空值。
我當前的代碼如下所示:
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
PrincipalSearchResult<Principal> rsc = srch.FindAll();
foreach (Principal found in rsc)
{
using (PrincipalContext ctx2 = new PrincipalContext(ContextType.Domain, "nianetas.local"))
{
UserPrincipal usr = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, found.SamAccountName);
DateTime creationTime;
string creTime = found.GetProperty("whenCreated");
if (!string.IsNullOrEmpty(creTime))
{
creationTime = DateTime.ParseExact(creTime,
"dd-MM-yyyy HH:mm:ss", null);
}
else
{
creationTime = DateTime.MinValue;
}
}
}
GetProperty函數:
public static String GetProperty(this Principal principal, String property)
{
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties.Contains(property))
return directoryEntry.Properties[property].Value.ToString();
else
return String.Empty;
}
嘗試使用帶有whenCreated
屬性的DirectoryEntry
對象的RefreshCache
方法。 看起來當使用GetUnderlyingObject()
並非所有DirectoryEntry
屬性都被填充,因為這是一項繁重的操作。
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
directoryEntry.RefreshCache(new string[] { "whenCreated" });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.