[英]UserPrincipal AccountLockoutTime always null
我正在访问Active Directory中用户对象的各种属性。 我有下面写的方法。
它适用于除AccountLockoutTime以外的所有属性,该属性始终返回为null。
public IEnumerable<ActiveDirectoryAccount> GetUserAccounts(string samAccountName)
{
PrincipalContext pricipalContext = new PrincipalContext(ContextType.Domain, "domainname.co.za:3268");
UserPrincipal userPrincipal = new UserPrincipal(pricipalContext);
userPrincipal.SamAccountName = "*" + samAccountName + "*";
PrincipalSearcher principalSearcher = new PrincipalSearcher(userPrincipal);
ICollection<ActiveDirectoryAccount> result = new List<ActiveDirectoryAccount>();
foreach (UserPrincipal userSearchResult in principalSearcher.FindAll())
{
ActiveDirectoryAccount account = new ActiveDirectoryAccount()
{
AccountLockedOut = userSearchResult.IsAccountLockedOut(),
DistinguishedName = userSearchResult.DistinguishedName,
Description = userSearchResult.Description,
Enabled = userSearchResult.Enabled,
GUID = userSearchResult.Guid,
LastLogon = userSearchResult.LastLogon,
LastPasswordSet = userSearchResult.LastPasswordSet,
// The below line always comes back as null
LockoutTime = userSearchResult.AccountLockoutTime,
PasswordNeverExpires = userSearchResult.PasswordNeverExpires,
SAMAccountName = userSearchResult.SamAccountName,
SmartcardLogonRequired = userSearchResult.SmartcardLogonRequired,
UserCannotChangePassword = userSearchResult.UserCannotChangePassword,
UserPrincipalName = userSearchResult.UserPrincipalName
};
if (userSearchResult.GetUnderlyingObjectType() == typeof(DirectoryEntry))
{
using (DirectoryEntry entry = (DirectoryEntry)userSearchResult.GetUnderlyingObject())
{
account.WhenChanged = (DateTime)entry.Properties["whenChanged"].Value;
account.WhenCreated = (DateTime)entry.Properties["whenCreated"].Value;
// Tried the below to get the data as well but no luck.
if (userSearchResult.IsAccountLockedOut())
{
if (entry.Properties["lockoutTime"].Value != null)
{
account.Test = (string)entry.Properties["lockoutTime"].Value;
}
}
}
}
result.Add(account);
}
principalSearcher.Dispose();
return result.ToList();
}
我已锁定一个帐户,以检查上面的代码是否可以读取IsAccountLockedOut。 它可以并且返回true。 对于userSearchResult.AccountLockoutTime
或(string)entry.Properties["lockoutTime"].Value;
它始终返回null (string)entry.Properties["lockoutTime"].Value;
我已经检查了Active Directory中的lockoutTime属性,并在锁定帐户时为用户帐户填充了该属性。
关于出什么问题有什么想法吗?
提前致谢。 :)
克里斯
通过entry.Properties["lockoutTime"].Value
,lockoutTime属性是一个支持IADsLargeInteger接口的COM对象。
您可以在此处使用代码来获取其价值:
[ComImport,
InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
Guid("9068270B-0939-11D1-8BE1-00C04FD8D503")]
public interface IADsLargeInteger
{
int HighPart{get;set;}
int LowPart{get;set;}
}
private DateTime? GetLockoutTime(DirectoryEntry de)
{
DateTime? ret = null;
IADsLargeInteger largeInt = de.Properties["lockoutTime"].Value as IADsLargeInteger;
if (largeInt != null)
{
long ticks = (long)largeInt.HighPart << 32 | largeInt.LowPart;
// 0 means not lockout
if (ticks != 0)
{
ret = DateTime.FromFileTimeUtc(ticks.Value);
}
}
return ret;
}
请注意, lockoutTime
的值是帐户被锁定的时间,而不是“锁定至”时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.