title: Lock Active Directory accounts programmatically
body: <p>I have to lock user accounts in Active Directory programmatically in C#. </p>
  
  <p>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).</p>
  
  <p>Now I tried to lock the account by providing incorrect credentials (see below), but this doesn't work either.</p>
  
  <pre><code>int retries = 0;
  while (!adsUser.IsAccountLocked &amp;&amp; retries &lt; MAX_LOCK_RETRIES)
  {
       retries++;
  
      try
      {  
          new DirectoryEntry(userPath, logonName, incorrectPassword).RefreshCache();
      }
      catch (Exception)
      { 
          /* ... */ 
      }
      adsUser.GetInfo();
  }
  </code></pre>
  
  <p>Any ideas?</p>