public void ResetPassword(string aUsername, string aPassword, string tarUsername, string tarNpwd)
 {
     try
     {
         Impersonation im = new Impersonation();
         if (im.impersonateValidUser(aUsername, _servername, aPassword))
         {
             using (DirectoryEntry entry = new DirectoryEntry("WinNT://" + _servername + ",computer"))
             {
                 entry.Children.SchemaFilter.Add("User");
                 if (tarUsername.Contains("\\"))
                 {
                     tarUsername = tarUsername.Substring(tarUsername.LastIndexOf('\\') + 1);
                 }
                 foreach (DirectoryEntry _child in entry.Children)
                 {
                     if (_child.Name.Equals(tarUsername, StringComparison.OrdinalIgnoreCase))
                     {
                         //Bind to the native AdsObject to force authentication.
                         //object obj = entry.NativeObject;
                         //invoke change password
                         _child.Invoke("SetPassword", new object[] { tarNpwd });
                         //entry.Invoke("ChangePassword", new object[] { opwd, npwd });
                         //entry.Properties["LockOutTime"].Value = 0;
                         _child.CommitChanges();
                         _child.Close();
                     }
                 }
             }
             im.undoImpersonation();
         }
     }
     catch (COMException ex)
     {
         throw new Exception("COMError resetpassword user. " + ex.ToString());
     }
     catch (Exception ex)
     {
         throw new Exception("Error resetpassword user. " + ex.ToString());
     }
 }
 public void ResetPassword(string adomain, string adminsUser, string adminPass, string targetUsername, string targetPass)
 {
     try
     {
         Impersonation im = new Impersonation();
         if (im.impersonateValidUser(adminsUser, adomain, adminPass))
         {
             string domainAndUsername = adomain + @"\" + adminsUser;
             if (adminsUser.Contains(@"\"))
             {
                 domainAndUsername = adminsUser;
             }
             using (DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, adminPass, AuthenticationTypes.Secure))
             {
                 var userEntry = new DirectorySearcher(entry)
                 {
                     SearchRoot = entry, Filter = "(&(objectCategory=user)(cn=" + targetUsername + "))"
                 };
                 //Bind to the native AdsObject to force authentication.
                 object obj = entry.NativeObject;
                 //invoke change password
                 entry.Invoke("SetPassword", new object[] { targetPass });
                 //entry.Invoke("ChangePassword", new object[] { opwd, npwd });
                 entry.Properties["LockOutTime"].Value = 0;
                 entry.Close();
                 //entry.CommitChanges();
             }
             im.undoImpersonation();
         }
     }
     catch (COMException ex)
     {
         throw new Exception("COMError resetpassword user. " + ex.ToString());
     }
     catch (Exception ex)
     {
         throw new Exception("Error resetpassword user. " + ex.ToString());
     }
 }