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;
}



C# Retrieve Get Directory User Attributes

UserPrincipal user = UserPrincipal.FindByIdentity(GetPrincipalContext(), IdentityType.SamAccountName, alias);
if (user != null)
{   
    DirectoryEntry directoryEntry = (user.GetUnderlyingObject() as DirectoryEntry);
    DirectorySearcher adSearch = new DirectorySearcher(directoryEntry);   
    adSearch.SearchScope = SearchScope.Subtree;
    adSearch.Filter = "(&(ObjectClass=user)(sAMAccountName=" + alias + "))";
    SearchResult sResult = adSearch.FindOne();

    if (sResult.Properties.Contains(attribute))
        propertyValue = sResult.Properties[attribute][0].ToString();
}


Upgrade Windows 11 Home to Windows 11 Pro

Disable internet connection (Wi-Fi, Internet, etc.) Change the product key using the following Generic product key:                     VK7J...