Active Directory のユーザー オブジェクトのさまざまなプロパティにアクセスしています。私が書いた以下の方法があります。
これは、常に null として返される AccountLockoutTime を除くすべてのプロパティで機能します。
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();
}
I have locked an account out to check if the code above can read IsAccountLockedOut. It can and returns true. It always returns null for userSearchResult.AccountLockoutTime
or (string)entry.Properties["lockoutTime"].Value;
I have checked the lockoutTime property in Active Directory and it is populated for the user account when I lock the account out.
Any ideas as to what is going wrong?
Thanks in advance. :)
Chris