views:

159

answers:

4

Hello,

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!

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

if( ((int)result.Properties["useraccountcontrol"]) & AccountFlags.AccountDisable > 0 )
{
    // Account is Disabled...
}
TJMonk15
+4  A: 
Convert.ToBoolean((int)result.Properties["useraccountcontrol"] & 0x0002)

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

flq
All good answers so far, but thanks Frank - I think you were first!
Jim
+2  A: 

Here is great tutorial for Active Directory via C# in general (via).

Basically you want to check the number with the & operator:

if( ( result & ACCOUNTDISABLE ) == ACCOUNTDISABLE )
{ .... }

You can define ACCOUNTDISABLE as a const (like in the table you linked). Or you create an enum for the values. Make sure to assign the right numbers and set a [Flags]-attribute.
What you chose depends on how many values you need. When dealing only with ACCOUNTDISABLE and nothing else, a define or const will do.

tanascius
+1  A: 
UserAccountControlFlags userAccFlags = (UserAccountControlFlags) 66050;

// Much more readable    
if(userAccFlags.Has(UserAccountControlFlags.AccountDisabled))
{
   // Do your stuff here
}

And here's the extension method:

public static bool Has<T>(this System.Enum type, T value) where T : struct 
{
    return ((int)(object)type & (int)(object)value) > 0;
}

Enum definition refered from http://goo.gl/5Db5

[Flags]
public enum UserAccountControlFlags
{ 
  Script                             = 0x1,
  AccountDisabled                    = 0x2,
  HomeDirectoryRequired              = 0x8,
  AccountLockedOut                   = 0x10,
  PasswordNotRequired                = 0x20,
  PasswordCannotChange               = 0x40,
  EncryptedTextPasswordAllowed       = 0x80,
  TempDuplicateAccount               = 0x100,
  NormalAccount                      = 0x200,
  InterDomainTrustAccount            = 0x800,
  WorkstationTrustAccount            = 0x1000,
  ServerTrustAccount                 = 0x2000,
  PasswordDoesNotExpire              = 0x10000,
  MnsLogonAccount                    = 0x20000,
  SmartCardRequired                  = 0x40000,
  TrustedForDelegation               = 0x80000,
  AccountNotDelegated                = 0x100000,
  UseDesKeyOnly                      = 0x200000,
  DontRequirePreauth                 = 0x400000,
  PasswordExpired                    = 0x800000,
  TrustedToAuthenticateForDelegation = 0x1000000,
  NoAuthDataRequired                 = 0x2000000
}
Vivek