// 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);
}
}