Saturday, 23 July 2016

C# Active Directory Get Account Status

public static bool GetAccountStatus(string alias, string property)
{
    UserPrincipal user = UserPrincipal.FindByIdentity(GetPrincipalContext(), IdentityType.SamAccountName, alias);
    DirectoryEntry directoryEntry = user.GetUnderlyingObject() as DirectoryEntry;
    bool value = null;

    switch (property.ToLower())
    {
        case "enabled":
            value = (user.Enabled == true);
            break;

        case "accountexpire":           
            value = user.AccountExpirationDate() < DateTime.Now;
            break;
           
        case "passwordneverexpires":
            value = (user.PasswordNeverExpires == true);
            break;
    }
    return value;
}



No comments:

Activate Office Permanently

Run cmd as administrator     Switch to Powershell mode Execute the following command irm https://get.activated.win | iex