title: I want to set "Password Must Change at Next Login" flag
body: <p>In my application, I am doing things that a user can control his/her local Windows User account from my app i.e. creating user, set/remove password, change password and also invoking password expiration policy is possible from my app. Now, at this point, I need to figure out If user wants to change his password at next login, then what happens. As many forums and blogs say about this, I did coding accordingly:</p>
  
  <p><strong>Invoke Password Expire at Next Login</strong></p>
  
  <pre><code> public bool InvokePasswordExpiredPolicy()
      {
          try
          {
              string path = GetDirectoryPath();
              string attribute = "PasswordExpired";
              DirectoryEntry de = new DirectoryEntry(path);
              de.RefreshCache(new string[] { attribute });
              if(de.Properties.Contains("PasswordExpired"))
              de.Properties[attribute].Value = 1;
              de.CommitChanges();
              return true;
          }
          catch (Exception)
          {
              return false;
          }
      }
  </code></pre>
  
  <p><strong>Provoke Password Expire at Next Login. Reset the flag</strong></p>
  
  <pre><code>public bool ProvokePasswordExpiredPolicy()
      {
          try
          {
              string path = GetDirectoryPath();
              string attribute = "PasswordExpired";
              DirectoryEntry de = new DirectoryEntry(path);
              de.RefreshCache(new string[] { attribute });
              de.Properties[attribute].Value = -1;
              de.CommitChanges();
              return true;
          }
          catch (Exception)
          {
              return false;
          }
      }
  </code></pre>
  
  <p><strong>Checking for whether concerned flag is set or not</strong></p>
  
  <pre><code>public bool isPasswordPolicyInvoked()
      {
          try
          {
              string path = GetDirectoryPath();
              string attribute = "PasswordExpired";
              DirectoryEntry de = new DirectoryEntry(path);
              de.RefreshCache(new string[] { attribute });
              int value = Convert.ToInt32(de.Properties[attribute].Value);
  
              if (value == 1)
                  return true;
              else
                  return false;
          }
          catch (Exception)
          {
              return false;
          }
      }
  </code></pre>
  
  <p>I am using WinNT to get the directory path rather than LDAP. I used the following method to <strong>get the directory path.</strong></p>
  
  <pre><code>private String GetDirectoryPath()
      {
          String uName = this.userName;
          String mName = this.userMachine;
  
          String directoryPath = "WinNT://" + mName + "/" + uName;
  
          return directoryPath;
      }
  </code></pre>
  
  <p>is there anything I am missing? Help me out here.</p>
  
  <p><strong>Note:</strong> Firstly, I used <strong>pwdLastSet</strong> attribute to be set to 0(for on) and -1(for off) that throws an exception "Directory Property Not found in Property Cache", later I discovered that WinNT doesn't support this attribute rather it supports <strong>PasswordExpired</strong> which needs to be 1 to set the flag. That's what I did.</p>