Exemplo n.º 1
0
    /// <summary>
    /// Effectue une requête sur l'API (bitbucket ou devops) et retourne le contenue sous forme de chaîne de caractère.
    /// </summary>
    public async Task <string> QueryAsync(string type, string url, string source)
    {
        if (entropy == "")
        {
            // Si le mot de passe n'a pas encore été rentré, le demande
            using (Password formOptions = new Password())
            {
                formOptions.ShowDialog();
                formOptions.Activate();
                try
                {
                    if (formOptions.pass.Length != 0)
                    {
                        entropy = formOptions.pass;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        try
        {
            // Effectue la requête
            byte[] ciphertext = File.ReadAllBytes(config.GetAppData() + @".cred" + source);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            WebRequest myReq = WebRequest.Create(url);
            myReq.Method = "GET";
            CredentialCache mycache = new CredentialCache();
            if (type == "bitbucket")
            {
                myReq.Headers["Authorization"] = "Basic " + Convert.ToBase64String(
                    Encoding.ASCII.GetBytes(
                        config.GetUserName(source) + ":" + Encoding.UTF8.GetString(ProtectedData.Unprotect(ciphertext, Encoding.Default.GetBytes(entropy), DataProtectionScope.CurrentUser))));
            }
            else if (type == "devops")
            {
                myReq.Headers["Authorization"] = "Basic " + Convert.ToBase64String(
                    ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "", Encoding.UTF8.GetString(ProtectedData.Unprotect(ciphertext, Encoding.Default.GetBytes(entropy), DataProtectionScope.CurrentUser)))));
            }
            WebResponse wr = await myReq.GetResponseAsync();

            Stream       receiveStream = wr.GetResponseStream();
            StreamReader reader        = new StreamReader(receiveStream, Encoding.UTF8);
            return(await reader.ReadToEndAsync());
        }
        catch (CryptographicException ex)
        {
            entropy = "";
            if (MessageBox.Show("Mot de passe incorrect", "Erreur", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry)
            {
                return(await QueryAsync(type, url, source));
            }
            Console.WriteLine(type + " query error : " + ex.Message);
        }
        catch (WebException) { Console.WriteLine(source + " web error : " + url); }
        return("");
    }