UserPrincipal AccountLockoutTime始终为null

时间:2015-01-20 17:05:20

标签: c# active-directory userprincipal

我正在访问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

我已经检查了Active Directory中的lockoutTime属性,并在我锁定帐户时填充了用户帐户。

关于出了什么问题的任何想法?

提前致谢。 :)

克里斯

1 个答案:

答案 0 :(得分:1)

通过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的值是帐户被锁定的时间,但不是&#34;锁定到&#34;时间。