used for encryption and decryption
Beispiel #1
0
        // 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);
            }
        }
Beispiel #2
0
        // 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);
            }
        }
Beispiel #3
0
        void worker_DoWork <T>(object sender, DoWorkEventArgs e) where T : new()
        {
            try
            {
                var client = JiraClient.Client;
                if (!usebaseurl)
                {
                    client = new RestClient();
                    client.CookieContainer = JiraClient.Client.CookieContainer;
                    string usernameS = MySettings.Get("username");
                    string passwordS = DataProtector.DecryptData(MySettings.Get("password"));
                    client.Authenticator = new HttpBasicAuthenticator(usernameS, passwordS);
                }
                BackgroundWorker worker = (BackgroundWorker)sender;
                if (requests.Count > 1)
                {
                    for (var i = 0; i < requests.Count; i++)
                    {
                        worker.ReportProgress((100 * i + 1) / requests.Count(), getProgressString(i + 1));
                        // HAS TO BE OUT OF THE DISPATCHER!

                        var response = client.Execute <T>(requests[i]);
                        responses.Add(response);
                        if (!RestCallback.Check(response))
                        {
                            break;
                        }

                        if (i == requests.Count() - 1)
                        {
                            worker.ReportProgress(100, getProgressString(i + 1));
                        }
                    }
                }
                else
                {
                    var response = client.Execute <T>(requests[0]);
                    responses.Add(response);
                    RestCallback.Check(response);
                }
            }
            catch (System.Exception ex1)
            {
                MessageBox.Show("exception: " + ex1);
            }
        }