Checking if a user account is enabled in .NET

Here's a handy little code snippet to figure out if a local windows user account is enabled or not using the System.DirectoryServices namespace.

private static bool IsUserAccountEnabled(string username)
{
    try
    {
        var result = new DirectoryEntry { Path = "WinNT://" + Environment.MachineName + ",computer" }
            .Children
            .Cast<DirectoryEntry>()
            .Where(d => d.SchemaClassName == "User")
            .First(d => d.Properties["Name"].Value.ToString() == username);

        return ((int)result.Properties["UserFlags"].Value & 2) != 2;
    }
    catch
    {
        return false;
    }
}

~Eoin Campbell

Eoin Campbell

Eoin Campbell
Dad, Husband, Coder, Nerd. I work primarily on the Microsoft .NET & Azure Stack

Data Partitioning Strategy in Cosmos DB

Deciding how to partition your data in Cosmos DB is one of the most challenging architecture/design decisions Continue reading

Read to end using stdin from the console

Published on May 30, 2018

Qluent

Published on May 10, 2018