Пример #1
0
        public void Initialize(IIssueTrackerWatcher issueTrackerWatcher, ISettingsSource config)
        {
            if (this._issueTrackerWatcher != null)
            {
                throw new InvalidOperationException("Already initialized");
            }

            this._issueTrackerWatcher = issueTrackerWatcher;

            _issueCache = new SqlCeIssueTrackerCache();
            _issueCache.Initialize(REFRESH_INTERVAL_MINS);

            var hostName = config.GetString("ServerUrl", null);
            var user     = config.GetString("Username", null);
            var password = config.GetString("Password", null);

            if (!string.IsNullOrEmpty(hostName))
            {
                _httpClient = new HttpClient
                {
                    Timeout     = TimeSpan.FromMinutes(2),
                    BaseAddress = hostName.Contains("://")
                                          ? new Uri(hostName, UriKind.Absolute)
                                          : new Uri(string.Format("{0}://{1}", Uri.UriSchemeHttp, hostName), UriKind.Absolute)
                };

                _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                IIssueTrackerCredentials issueTrackerCredentials = new IssueTrackerCredentials {
                    Username = user, Password = password
                };

                if (issueTrackerCredentials.Username == null || issueTrackerCredentials.Password == null)
                {
                    issueTrackerCredentials = issueTrackerWatcher.GetIssueTrackerCredentials(this, true);
                }


                UpdateHttpClientOptions(issueTrackerCredentials, CancellationToken.None);
            }
        }
Пример #2
0
        public IIssueTrackerCredentials GetIssueTrackerCredentials(IIssueTrackerAdapter IssueTrackerAdapter, bool useStoredCredentialsIfExisting)
        {
            lock (IssueTrackerCredentialsLock)
            {
                IIssueTrackerCredentials IssueTrackerCredentials = new IssueTrackerCredentials();

                const string CredentialsConfigName = "Credentials";

                const string UsernameKey = "Username";
                const string PasswordKey = "Password";
                using (var stream = GetIssueTrackerOptionsIsolatedStorageStream(IssueTrackerAdapter, FileAccess.Read, FileShare.Read))
                {
                    if (stream.Position < stream.Length)
                    {
                        var protectedData = new byte[stream.Length];

                        stream.Read(protectedData, 0, (int)stream.Length);

                        byte[] unprotectedData = ProtectedData.Unprotect(protectedData, null, DataProtectionScope.CurrentUser);
                        using (var memoryStream = new MemoryStream(unprotectedData))
                        {
                            ConfigFile credentialsConfig = new ConfigFile("", false);

                            using (var textReader = new StreamReader(memoryStream, Encoding.UTF8))
                            {
                                credentialsConfig.LoadFromString(textReader.ReadToEnd());
                            }

                            ConfigSection section = credentialsConfig.FindConfigSection(CredentialsConfigName);

                            if (section != null)
                            {
                                IssueTrackerCredentials.Username = section.GetValue(UsernameKey);
                                IssueTrackerCredentials.Password = section.GetValue(PasswordKey);

                                if (useStoredCredentialsIfExisting)
                                {
                                    return(IssueTrackerCredentials);
                                }
                            }
                        }
                    }
                }

                if (!useStoredCredentialsIfExisting)
                {
                    IssueTrackerCredentials = ShowIssueTrackerCredentialsForm(IssueTrackerAdapter.UniqueKey, IssueTrackerCredentials);

                    if (IssueTrackerCredentials != null)
                    {
                        ConfigFile credentialsConfig = new ConfigFile("", true);

                        ConfigSection section = credentialsConfig.FindOrCreateConfigSection(CredentialsConfigName);

                        section.SetValue(UsernameKey, IssueTrackerCredentials.Username);
                        section.SetValue(PasswordKey, IssueTrackerCredentials.Password);

                        using (var stream = GetIssueTrackerOptionsIsolatedStorageStream(IssueTrackerAdapter, FileAccess.Write, FileShare.None))
                        {
                            using (var memoryStream = new MemoryStream())
                            {
                                using (var textWriter = new StreamWriter(memoryStream, Encoding.UTF8))
                                {
                                    textWriter.Write(credentialsConfig.GetAsString());
                                }

                                var protectedData = ProtectedData.Protect(memoryStream.ToArray(), null, DataProtectionScope.CurrentUser);
                                stream.Write(protectedData, 0, protectedData.Length);
                            }
                        }

                        return(IssueTrackerCredentials);
                    }
                }

                return(null);
            }
        }