Exemple #1
0
        /// <summary>
        /// YouTrack issue reporter
        /// </summary>
        /// <param name="username">YouTrack username</param>
        /// <param name="password">YouTrack password</param>
        /// <param name="youTrackUri">YouTrack endpoint</param>
        /// <param name="authImmediately">If set to true, ensure auth cookie can be obtained on construction (synchronous) or throw.</param>
        public YouTrackReporter(string username, SecureString password, Uri youTrackUri, bool authImmediately = false)
        {
            if (string.IsNullOrEmpty(username))
            {
                throw new ArgumentException(nameof(username));
            }

            if (password == null)
            {
                throw new ArgumentNullException(nameof(password));
            }

            if (youTrackUri == null)
            {
                throw new ArgumentNullException(nameof(youTrackUri));
            }

            var handler = new HttpClientHandler()
            {
                CookieContainer = new CookieContainer()
            };

            auth = async() =>
            {
                var cookie = handler.CookieContainer.GetCookies(youTrackUri)
                             .OfType <Cookie>()
                             .FirstOrDefault(x => x.Name.Equals(YouTrackContracts.AuthCookie, StringComparison.Ordinal));

                if (cookie == null || cookie.Expired)
                {
                    var response = await client
                                   .PostAsync(YouTrackContracts.Auth, new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair <string, string>("login", username),
                        new KeyValuePair <string, string>("password", SecureStringHelper.ToString(password)),
                    }))
                                   .ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        throw new InvalidOperationException(
                                  $"Authenticating through {response.RequestMessage.RequestUri}. Response {(int) response.StatusCode}: {response.ReasonPhrase}{Environment.NewLine}Body: {await response.Content.ReadAsStringAsync()}");
                    }

                    return(true);
                }

                return(true);
            };

            client = new HttpClient(handler, true)
            {
                BaseAddress = youTrackUri
            };

            // ReSharper disable once ImplicitlyCapturedClosure
            void OnDispose()
            {
                password.Dispose();
                client.Dispose();
            }

            if (authImmediately)
            {
                try
                {
                    auth().Wait();
                }
                catch
                {
                    OnDispose();
                    throw;
                }
            }

            onDispose = OnDispose;
        }
Exemple #2
0
 /// <summary>
 /// YouTrack issue reporter
 /// </summary>
 /// <param name="username">YouTrack username</param>
 /// <param name="password">YouTrack password</param>
 /// <param name="youTrackUri">YouTrack endpoint</param>
 /// <param name="authImmediately">If set to true, ensure auth cookie can be obtained on construction (synchronous) or throw.</param>
 public YouTrackReporter(string username, string password, Uri youTrackUri, bool authImmediately = false)
     : this(username, SecureStringHelper.ToSecureString(password), youTrackUri, authImmediately)
 {
 }