我目前正试图从我的广告中获取不同的信息。 但是我在拉动用户的创建日期时遇到了一些问题。
我在某些用户上获得了正确的日期,但在其他用户上我得到了null。
我目前的代码如下:
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;
}
答案 0 :(得分:2)
尝试将DirectoryEntry
对象的RefreshCache
方法与whenCreated
属性一起使用。看起来在使用GetUnderlyingObject()
时,不会填充所有DirectoryEntry
属性,因为它是一个繁重的操作。
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
directoryEntry.RefreshCache(new string[] { "whenCreated" });