コード例 #1
0
        public static async Task Run(string tag, string value, AasxIntegrationBase.IFlyoutProvider flyoutProvider)
        {
            ServicePointManager.ServerCertificateValidationCallback =
                new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

            // Initializes the variables to pass to the MessageBox.Show method.
            string caption = "Connect with " + tag + ".dat";
            string message = "";

            bool withOpenidFile = false;

            if (value != "")
            {
                dataServer = value;
                authServer = "";
                certPfx    = "";
                certPfxPW  = "";
                value      = "";
            }
            else
            {
                // read openx.dat
                try
                {
                    using (StreamReader sr = new StreamReader(tag + ".dat"))
                    {
                        authServer = sr.ReadLine();
                        dataServer = sr.ReadLine();
                        certPfx    = sr.ReadLine();
                        certPfxPW  = sr.ReadLine();
                        outputDir  = sr.ReadLine();
                    }
                }
                catch (Exception ex)
                {
                    AdminShellNS.LogInternally.That.Error(ex, $"The file {tag}.dat can not be read.");
                    return;
                }
                withOpenidFile = true;
            }

            message =
                "authServer: " + authServer + "\n" +
                "dataServer: " + dataServer + "\n" +
                "certPfx: " + certPfx + "\n" +
                "certPfxPW: " + certPfxPW + "\n" +
                "outputDir: " + outputDir + "\n" +
                "\nConinue?";

            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult      result;

            // Displays the MessageBox.
            result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);
            if (result != System.Windows.Forms.DialogResult.Yes)
            {
                // Closes the parent form.
                return;
            }

            System.Windows.Forms.MessageBox.Show("Access Aasx Server at " + dataServer,
                                                 "Data Server", MessageBoxButtons.OK);

            var handler = new HttpClientHandler();

            handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
            handler.AllowAutoRedirect       = false;
            var client = new HttpClient(handler)
            {
                BaseAddress = new Uri(dataServer)
            };

            if (token != "")
            {
                client.SetBearerToken(token);
            }

            string operation     = "";
            string lastOperation = "";

            if (withOpenidFile)
            {
                operation     = "authenticate";
                lastOperation = "/server/listaas/";
            }
            else
            {
                operation = "/server/listaas/";
            }

            while (operation != "" && operation != "error")
            {
                System.Windows.Forms.MessageBox.Show("operation: " + operation + value + "\ntoken: " + token,
                                                     "Operation", MessageBoxButtons.OK);

                switch (operation)
                {
                case "/server/listaas/":
                case "/server/getaasx2/":
                    try
                    {
                        HttpResponseMessage response2 = null;
                        switch (operation)
                        {
                        case "/server/listaas/":
                            response2 = await client.GetAsync(operation);

                            break;

                        case "/server/getaasx2/":
                            response2 = await client.GetAsync(operation + value);

                            break;
                        }

                        if (response2.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                        {
                            string   redirectUrl = response2.Headers.Location.ToString();
                            string[] splitResult = redirectUrl.Split(new string[] { "?" },
                                                                     StringSplitOptions.RemoveEmptyEntries);
                            Console.WriteLine("Redirect to:" + splitResult[0]);
                            authServer = splitResult[0];
                            System.Windows.Forms.MessageBox.Show(authServer, "Redirect to", MessageBoxButtons.OK);
                            lastOperation = operation;
                            operation     = "authenticate";
                            continue;
                        }
                        if (!response2.IsSuccessStatusCode)
                        {
                            lastOperation = operation;
                            operation     = "error";
                            continue;
                        }
                        String urlContents = await response2.Content.ReadAsStringAsync();

                        switch (operation)
                        {
                        case "/server/listaas/":
                            var      listOfAas = new List <SelectFromListFlyoutItem>();
                            string[] split     = urlContents.Split('\n');
                            int      i         = 0;
                            int      offset    = 0;
                            foreach (string s in split)
                            {
                                if (offset >= 2 && offset < split.Length - 2)
                                {
                                    listOfAas.Add(new SelectFromListFlyoutItem(s, new Tuple <int>(i++)));
                                }
                                offset++;
                            }
                            if (listOfAas.Count < 1)
                            {
                                System.Windows.Forms.MessageBox.Show(
                                    "No AAS found. Aborting.", "Select AAS to get ..",
                                    MessageBoxButtons.OK);
                                return;
                            }
                            var uc = new SelectFromListFlyout();
                            uc.Caption     = "Select AAS to get ..";
                            uc.ListOfItems = listOfAas;
                            flyoutProvider.StartFlyoverModal(uc);
                            if (uc.ResultItem != null && uc.ResultItem.Tag != null &&
                                uc.ResultItem.Tag is Tuple <int> )
                            {
                                // get result arguments
                                var TagTuple = uc.ResultItem.Tag as Tuple <int>;
                                value = TagTuple.Item1.ToString();
                            }
                            else
                            {
                                System.Windows.Forms.MessageBox.Show(
                                    "No AAS found. Aborting.", "Select AAS to get ..",
                                    MessageBoxButtons.OK);
                                return;
                            }
                            operation = "/server/getaasx2/";
                            break;

                        case "/server/getaasx2/":
                            try
                            {
                                var parsed3 = JObject.Parse(urlContents);

                                string fileName = parsed3.SelectToken("fileName").Value <string>();
                                string fileData = parsed3.SelectToken("fileData").Value <string>();

                                var enc         = new System.Text.ASCIIEncoding();
                                var fileString4 = Jose.JWT.Decode(fileData, enc.GetBytes(secretString),
                                                                  JwsAlgorithm.HS256);
                                var parsed4 = JObject.Parse(fileString4);

                                string binaryBase64_4 = parsed4.SelectToken("file").Value <string>();
                                Byte[] fileBytes4     = Convert.FromBase64String(binaryBase64_4);

                                Console.WriteLine("Writing file: " + outputDir + "\\" + "download.aasx");
                                File.WriteAllBytes(outputDir + "\\" + "download.aasx", fileBytes4);
                            }
                            catch (Exception ex)
                            {
                                AdminShellNS.LogInternally.That.Error(ex, $"Failed at operation: {operation}");
                                lastOperation = operation;
                                operation     = "error";
                            }
                            operation = "";
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        AdminShellNS.LogInternally.That.Error(ex, $"Failed at operation: {operation}");
                        lastOperation = operation;
                        operation     = "error";
                    }
                    break;

                case "authenticate":
                    try
                    {
                        X509SigningCredentials x509Credential = null;
                        if (withOpenidFile)
                        {
                            x509Credential = new X509SigningCredentials(new X509Certificate2(certPfx, certPfxPW));
                        }

                        var response = await RequestTokenAsync(x509Credential);

                        token = response.AccessToken;
                        client.SetBearerToken(token);

                        response.Show();
                        System.Windows.Forms.MessageBox.Show(response.AccessToken,
                                                             "Access Token", MessageBoxButtons.OK);

                        operation     = lastOperation;
                        lastOperation = "";
                    }
                    catch (Exception ex)
                    {
                        AdminShellNS.LogInternally.That.Error(ex, $"Failed at operation: {operation}");
                        lastOperation = operation;
                        operation     = "error";
                    }
                    break;

                case "error":
                    System.Windows.Forms.MessageBox.Show($"Can not perform: {lastOperation}",
                                                         "Error", MessageBoxButtons.OK);
                    break;
                }
            }
        }
コード例 #2
0
        public static async Task Run(string tag, string value, AasxIntegrationBase.IFlyoutProvider flyoutProvider)
        {
            ServicePointManager.ServerCertificateValidationCallback =
                new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

            // Initializes the variables to pass to the MessageBox.Show method.
            string caption = "Connect with " + tag + ".dat";
            string message = "";

            // read openx.dat
            try
            {
                using (StreamReader sr = new StreamReader(tag + ".dat"))
                {
                    authServer = sr.ReadLine();
                    dataServer = sr.ReadLine();
                    certPfx    = sr.ReadLine();
                    certPfxPW  = sr.ReadLine();
                    outputDir  = sr.ReadLine();

                    message =
                        "authServer: " + authServer + "\n" +
                        "dataServer: " + dataServer + "\n" +
                        "certPfx: " + certPfx + "\n" +
                        "certPfxPW: " + certPfxPW + "\n" +
                        "outputDir: " + outputDir + "\n" +
                        "\nConinue?";
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(tag + ".dat " + " can not be read!");
                return;
            }

            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult      result;

            // Displays the MessageBox.
            result = System.Windows.Forms.MessageBox.Show(message, caption, buttons);
            if (result != System.Windows.Forms.DialogResult.Yes)
            {
                // Closes the parent form.
                return;
            }

            System.Windows.Forms.MessageBox.Show("Access Aasx Server at " + dataServer,
                                                 "Data Server", MessageBoxButtons.OK);

            var handler = new HttpClientHandler();

            handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
            handler.AllowAutoRedirect       = false;
            var client = new HttpClient(handler)
            {
                BaseAddress = new Uri(dataServer)
            };

            if (token != "")
            {
                client.SetBearerToken(token);
            }

            string operation     = "/server/listaas/";
            string lastOperation = "";

            while (operation != "" && operation != "error")
            {
                System.Windows.Forms.MessageBox.Show("operation: " + operation + "\ntoken: " + token,
                                                     "Operation", MessageBoxButtons.OK);

                switch (operation)
                {
                case "/server/listaas/":
                case "/server/getaasx2/":
                    try
                    {
                        HttpResponseMessage response2 = null;
                        switch (operation)
                        {
                        case "/server/listaas/":
                            response2 = await client.GetAsync(operation);

                            break;

                        case "/server/getaasx2/":
                            response2 = await client.GetAsync(operation + value);

                            break;
                        }

                        if (response2.StatusCode == System.Net.HttpStatusCode.TemporaryRedirect)
                        {
                            string   redirectUrl = response2.Headers.Location.ToString();
                            string[] splitResult = redirectUrl.Split(new string[] { "?" },
                                                                     StringSplitOptions.RemoveEmptyEntries);
                            Console.WriteLine("Redirect to:" + splitResult[0]);
                            authServer = splitResult[0];
                            System.Windows.Forms.MessageBox.Show(authServer, "Redirect to", MessageBoxButtons.OK);
                            lastOperation = operation;
                            operation     = "authenticate";
                            continue;
                        }
                        if (!response2.IsSuccessStatusCode)
                        {
                            lastOperation = operation;
                            operation     = "error";
                            continue;
                        }
                        String urlContents = urlContents = await response2.Content.ReadAsStringAsync();

                        switch (operation)
                        {
                        case "/server/listaas/":
                            System.Windows.Forms.MessageBox.Show(urlContents, "/server/listaas/",
                                                                 MessageBoxButtons.OK);
                            var uc = new AasxPackageExplorer.TextBoxFlyout("REST server adress:",
                                                                           MessageBoxImage.Question);
                            uc.Text = "0";
                            flyoutProvider.StartFlyoverModal(uc);
                            if (uc.Result)
                            {
                                value = uc.Text;
                            }
                            operation = "/server/getaasx2/";
                            break;

                        case "/server/getaasx2/":
                            try
                            {
                                var parsed3 = JObject.Parse(urlContents);

                                string fileName = parsed3.SelectToken("fileName").Value <string>();
                                string fileData = parsed3.SelectToken("fileData").Value <string>();

                                var enc         = new System.Text.ASCIIEncoding();
                                var fileString4 = Jose.JWT.Decode(fileData, enc.GetBytes(secretString),
                                                                  JwsAlgorithm.HS256);
                                var parsed4 = JObject.Parse(fileString4);

                                string binaryBase64_4 = parsed4.SelectToken("file").Value <string>();
                                Byte[] fileBytes4     = Convert.FromBase64String(binaryBase64_4);

                                Console.WriteLine("Writing file: " + outputDir + "\\" + "download.aasx");
                                File.WriteAllBytes(outputDir + "\\" + "download.aasx", fileBytes4);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                                lastOperation = operation;
                                operation     = "error";
                            }
                            operation = "";
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        lastOperation = operation;
                        operation     = "error";
                    }
                    break;

                case "authenticate":
                    try
                    {
                        var certificate = new X509Certificate2(certPfx, certPfxPW);
                        X509SigningCredentials x509Credential = null;

                        var response = await RequestTokenAsync(x509Credential);

                        token = response.AccessToken;
                        client.SetBearerToken(token);

                        response.Show();
                        System.Windows.Forms.MessageBox.Show(response.AccessToken,
                                                             "Access Token", MessageBoxButtons.OK);

                        operation     = lastOperation;
                        lastOperation = "";
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        lastOperation = operation;
                        operation     = "error";
                    }
                    break;

                case "error":
                    Console.WriteLine("Can not " + lastOperation + "!");
                    System.Windows.Forms.MessageBox.Show("Can not " + lastOperation + "!",
                                                         "Error", MessageBoxButtons.OK);
                    break;
                }
            }
        }