public static void ForceChangeGooglePassword(String userName, String newPassword) { try { State.log.WriteLine("Initiating Google API Call"); using (GoogleDirectoryCall google = new GoogleDirectoryCall()) { State.log.WriteLine("Google API Call instantiated. Calling SetPassword"); google.SetPassword(userName, newPassword); State.log.WriteLine("SetPassword Completed."); } } catch (GoogleAPICall.GoogleAPIException gae) { throw gae; } }
public static void ChangeAllPasswords(string userName, string currentPassword, string newPassword, string domainName = "dublinschool.org", bool force = false) { AssertPasswordRequirements(newPassword); State.log.WriteLine("Password Requirements Met."); WebhostEventLog.Syslog.LogInformation("Password Requirements are met for {0} new password.", userName); if (force) { WebhostEventLog.Syslog.LogInformation("Forcibly changing password for {0}", userName); ForceChangeADPassword(userName, newPassword); } else { try { State.log.WriteLine("Connecting LDAP."); DirectoryEntry directionEntry = new DirectoryEntry("LDAP://192.168.76.3", domainName + "\\" + userName, currentPassword); if (directionEntry != null) { DirectorySearcher search = new DirectorySearcher(directionEntry); State.log.WriteLine("LDAP Connected, searching directory for SAMAccountName"); search.Filter = "(SAMAccountName=" + userName + ")"; SearchResult result = search.FindOne(); if (result != null) { State.log.WriteLine("Getting User Entry."); DirectoryEntry userEntry = result.GetDirectoryEntry(); if (userEntry != null) { State.log.WriteLine("Setting Password"); if (force) { userEntry.Invoke("SetPassword", new[] { newPassword }); } else { // Windows update broke this August 2016 // userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword }); // This is a fix that should work... ForceChangeADPassword(userName, newPassword); } userEntry.CommitChanges(); State.log.WriteLine("Changes Committed to ActiveDirectory."); WebhostEventLog.Syslog.LogInformation("Changes committed to ActiveDirectory for {0}", userName); } else { State.log.WriteLine("Could not get user Entry..."); WebhostEventLog.Syslog.LogError("Could not get user entry for {0}.", userName); } } else { State.log.WriteLine("Search returned no results."); WebhostEventLog.Syslog.LogError("Directory Search returned no results for {0}.", userName); } } else { State.log.WriteLine("Could not connect to LDAP with given username and passwd"); WebhostEventLog.Syslog.LogError("Could not connect to LDAP with the given password for {0}.", userName); } } catch (Exception ex) { WebhostEventLog.Syslog.LogError("Could not reset Windows password for {0}.{1}{2}", userName, Environment.NewLine, ex.Message); throw new PasswordException(String.Format("Failed to reset Windows Password for {0}.", userName), ex); } } try { State.log.WriteLine("Initiating Google API Call"); WebhostEventLog.GoogleLog.LogInformation("Connecting to Google API for a password change for {0}.", userName); using (GoogleDirectoryCall google = new GoogleDirectoryCall()) { State.log.WriteLine("Google API Call instantiated. Calling SetPassword"); WebhostEventLog.GoogleLog.LogInformation("Connected successfully with {0}.", userName); google.SetPassword(userName, newPassword); WebhostEventLog.GoogleLog.LogInformation("Successfully changed {0} password for Gmail.", userName); State.log.WriteLine("SetPassword Completed."); } } catch (GoogleAPICall.GoogleAPIException gae) { WebhostEventLog.GoogleLog.LogError("Failed to change password for {0}.{1}{2}", userName, Environment.NewLine, gae.Message); throw gae; } }