Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lock Active Directory accounts programmatically

I have to lock user accounts in Active Directory programmatically in C#.

Unfortunately it doesn't work via the userAccountControl attribute. Every time I set userAccountControl to 528 (=normal account w/ lockout flag), Active Directory won't accept the value and resets it without further notice to 512 (=normal account).

Now I tried to lock the account by providing incorrect credentials (see below), but this doesn't work either.

int retries = 0;
while (!adsUser.IsAccountLocked && retries < MAX_LOCK_RETRIES)
{
     retries++;

    try
    {  
        new DirectoryEntry(userPath, logonName, incorrectPassword).RefreshCache();
    }
    catch (Exception)
    { 
        /* ... */ 
    }
    adsUser.GetInfo();
}

Any ideas?

like image 542
Markus Dulghier Avatar asked Jul 23 '26 15:07

Markus Dulghier


1 Answers

Make sure the account you're using to disable the account has sufficient privileges to disable accounts. See this example from Microsoft.

like image 124
Nick DeVore Avatar answered Jul 26 '26 03:07

Nick DeVore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!