Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I (think) I want to use a BItWise Operator to check useraccountcontrol property!

Here's some code:

        DirectorySearcher searcher = new DirectorySearcher();
        searcher.Filter =  "(&(objectClass=user)(sAMAccountName=" + lstUsers.SelectedItem.Text + "))";
        SearchResult result = searcher.FindOne();

Within result.Properties["useraccountcontrol"] will be an item which will give me a value depending on the state of the account. For instance, a value of 66050 means I'm dealing with: A normal account; where the password does not expire;which has been disabled. Explanation here.

What's the most concise way of finding out if my value "contains" the AccountDisable flag (which is 2)

Thanks in advance!

like image 925
BIDeveloper Avatar asked Dec 04 '22 13:12

BIDeveloper


2 Answers

Convert.ToBoolean((int)result.Properties["useraccountcontrol"] & 0x0002)

Translated from a current code base here, it should work...

like image 188
flq Avatar answered Dec 08 '22 00:12

flq


enum AccountFlags
{
    Script = (1<<0),
    AccountDisable = (1<<1),
    // etc...
}

if( ((int)result.Properties["useraccountcontrol"]) & AccountFlags.AccountDisable > 0 )
{
    // Account is Disabled...
}
like image 22
FallenAvatar Avatar answered Dec 07 '22 23:12

FallenAvatar