// For using in the Settings window only public static List <JiraAccount> GetAllJiraAccounts() { try { Configuration config = GetConfig(); if (config == null) { return(null); } List <JiraAccount> allAccounts = new List <JiraAccount>(); foreach (string key in config.AppSettings.Settings.AllKeys) { if (key.StartsWith("jiraaccount_")) { try { JiraAccount ac = SimpleJson.DeserializeObject <JiraAccount>(config.AppSettings.Settings[key].Value); allAccounts.Add(ac); } catch (Exception ex) { allAccounts.Add(new JiraAccount() { active = false, jiraserver = string.Empty, username = string.Empty, password = string.Empty, guidfield = string.Empty, savedTime = (long)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds }); } } } // Compatibility for old version if (allAccounts.Count == 0) { JiraAccount ac = new JiraAccount() { active = true, jiraserver = Get("jiraserver"), username = Get("username"), password = Get("password"), guidfield = Get("guidfield"), savedTime = (long)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds }; // force to get a new config object to avoid conflict config = GetConfig(); allAccounts.Add(ac); string key = "jiraaccount_" + Convert.ToInt32(DateTime.UtcNow.AddHours(8).Subtract(new DateTime(1970, 1, 1)).TotalSeconds); string value = SimpleJson.SerializeObject(ac); config.AppSettings.Settings.Add(key, value); config.Save(ConfigurationSaveMode.Modified); } // Decrypt passwords allAccounts.ForEach(ac => ac.password = DataProtector.DecryptData(ac.password)); return(allAccounts); } catch (Exception ex) { MessageBox.Show("exception: " + ex); return(null); } }
/// <summary> /// Arup accounts need to change password every 3 months. Remind users to change here otherwise an account would be locked out until login on Jira. /// </summary> /// <returns>Always return false if not an Arup account</returns> public static bool isActiveAccountPasswordChanged() { if (MySettings.Get("jiraserver").Trim() == _jiraserverarup) { string username = MySettings.Get("username").Trim(); if (string.IsNullOrEmpty(username)) { return(false); } // get last saved time on user machine Configuration config = GetConfig(); if (config == null) { return(false); } DateTime currentTime = DateTime.Now; long lastSavedTime = (long)currentTime.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; foreach (string key in config.AppSettings.Settings.AllKeys) { if (key.StartsWith("jiraaccount_")) { try { JiraAccount ac = SimpleJson.DeserializeObject <JiraAccount>(config.AppSettings.Settings[key].Value); if (ac.username.Trim() == username && ac.jiraserver.Trim() == _jiraserverarup) { // fallback to old settings w/o password saved time if (ac.savedTime == 0) { ac.savedTime = GetWindowsAccountPasswordLastSetTime(ac.username, currentTime); } lastSavedTime = ac.savedTime; break; } } catch (Exception ex) { return(false); } } } // find last changed time on AD long lastChangedTime = GetWindowsAccountPasswordLastSetTime(username, currentTime); // return true if lastChangedTime is later than lastSavedTime if (lastChangedTime > lastSavedTime) { return(true); } } return(false); }
// For using in the Settings window only public static void SetAllJiraAccounts(List <JiraAccount> accounts) { try { Configuration config = GetConfig(); if (config == null) { return; } // Clear all exisiting accounts foreach (string key in config.AppSettings.Settings.AllKeys) { if (key.StartsWith("jiraaccount_")) { config.AppSettings.Settings.Remove(key); } } // Store all new account data foreach (JiraAccount ac in accounts) { if (!string.IsNullOrWhiteSpace(ac.jiraserver) || !string.IsNullOrWhiteSpace(ac.username) || !string.IsNullOrWhiteSpace(ac.password) || !string.IsNullOrWhiteSpace(ac.guidfield)) { ac.password = DataProtector.EncryptData(ac.password); DateTime currentTime = DateTime.Now; ac.savedTime = GetWindowsAccountPasswordLastSetTime(ac.username, currentTime); string key = "jiraaccount_" + Guid.NewGuid().ToString(); string value = SimpleJson.SerializeObject(ac); config.AppSettings.Settings.Add(key, value); config.Save(ConfigurationSaveMode.Modified); } } // Set active account JiraAccount activeAccount = accounts.Find(ac => ac.active); if (activeAccount != null) { Set("jiraserver", activeAccount.jiraserver); Set("username", activeAccount.username); Set("password", activeAccount.password); // no need to encrypt here, already did Set("guidfield", activeAccount.guidfield); } } catch (Exception ex) { MessageBox.Show("exception: " + ex); } }