private async System.Threading.Tasks.Task AcquireAccessTokenAsync(string ClientId)
        {
            Cursor  = Cursors.WaitCursor;
            Enabled = false;

            string[] scopes = Util.MailboxViewerScopes();

            _pca = PublicClientApplicationBuilder.Create(ClientId).Build();

            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                stringBuilder.AppendLine("MSAL - AcquireTokenAsync");
                stringBuilder.AppendLine("Application ID : " + ClientId);
                stringBuilder.AppendLine("Scope : " + string.Join(",", scopes));

                _ar = await _pca.AcquireTokenInteractive(scopes).WithPrompt(Prompt.ForceLogin).ExecuteAsync();

                stringBuilder.AppendLine("Result : Success");
                stringBuilder.AppendLine("AccessToken : " + (_ar.AccessToken ?? ""));
                stringBuilder.AppendLine("ExpiresOn : " + _ar.ExpiresOn.ToString());
                stringBuilder.AppendLine("IdToken : " + (_ar.IdToken ?? ""));
                stringBuilder.AppendLine("Scope : " + string.Join(",", _ar.Scopes));
                stringBuilder.AppendLine("UniqueId : " + (_ar.UniqueId ?? ""));
                stringBuilder.AppendLine("Username : "******""));
                stringBuilder.AppendLine("Identifier : " + (_ar.Account.HomeAccountId.Identifier ?? ""));
                stringBuilder.AppendLine("Name : " + (_ar.Account.Username ?? ""));

                Properties.Settings.Default.Save();
                DialogResult = DialogResult.OK;
                Close();
            }
            catch (Exception ex)
            {
                stringBuilder.AppendLine("Result : Fail");
                stringBuilder.AppendLine("Message : " + ex.Message);

                if (ex.Message != "User canceled authentication")
                {
                    MessageBox.Show(ex.Message, "Office365APIEditor");
                }
            }
            finally
            {
                Cursor  = Cursors.Default;
                Enabled = true;
            }

            Util.WriteSystemLog("AcquireViewerTokenForm", stringBuilder.ToString());
        }
Пример #2
0
        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            Util.WriteSystemLog("Office365APIEditor Closing Log", "Exit. Code 20");

#if !DEBUG
            if (!string.IsNullOrEmpty(Properties.Settings.Default.NewerInstallerPath) && File.Exists(Properties.Settings.Default.NewerInstallerPath) && Util.IsMsiDeployed)
            {
                // Newer installer is available

                if (MessageBox.Show("Do you want to install the latest version of Office365APIEditor?", "Office365APIEditor", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process msi = System.Diagnostics.Process.Start(Properties.Settings.Default.NewerInstallerPath);

                        Properties.Settings.Default.NewerInstallerPath = "";
                        Properties.Settings.Default.Save();
                    }
                    catch
                    {
                    }
                }
            }
            else if (!string.IsNullOrEmpty(Properties.Settings.Default.NewerInstallerPath) && Properties.Settings.Default.NewerInstallerPath == Util.LatestZipUri)
            {
                if (MessageBox.Show("Do you want to download the latest version of Office365APIEditor?", "Office365APIEditor", MessageBoxButtons.YesNo, MessageBoxIcon.None) == DialogResult.Yes)
                {
                    try
                    {
                        System.Diagnostics.Process zip = System.Diagnostics.Process.Start(Properties.Settings.Default.NewerInstallerPath);

                        Properties.Settings.Default.NewerInstallerPath = "";
                        Properties.Settings.Default.Save();
                    }
                    catch
                    {
                    }
                }
            }
#endif

            Environment.Exit(0);
        }
Пример #3
0
        static void Main(string[] args)
        {
            // Save the contents of the log temporarily until the initialization of the setting is completed.
            StringBuilder startupLog = new StringBuilder();

            startupLog.AppendLine("Start.");

            // Load previous settings.
            if (Properties.Settings.Default.NeedUpgrade == true)
            {
                startupLog.AppendLine("Setting upgrade is required.");

                try
                {
                    Properties.Settings.Default.Upgrade();
                    startupLog.AppendLine("Setting was upgraded.");
                }
                catch
                {
                    startupLog.AppendLine("Setting was not upgraded.");
                }
                finally
                {
                    Properties.Settings.Default.NeedUpgrade = false;
                    Properties.Settings.Default.Save();
                    startupLog.AppendLine("Setting was saved.");
                }
            }

            // Turn off SystemLogging flag.
            startupLog.AppendLine("Disable SystemLogging as default.");
            Properties.Settings.Default.SystemLogging = false;
            Properties.Settings.Default.Save();

            // Check the command line switches.
            startupLog.AppendLine("CommandLine : " + Environment.CommandLine);
            string[] switches = Environment.GetCommandLineArgs();

            bool systemLogging = false;

            foreach (string command in switches)
            {
                if (command.ToLower() == ("/ResetSettings").ToLower())
                {
                    // Reset all settings.
                    startupLog.AppendLine("Setting will be reset.");
                    Properties.Settings.Default.Reset();
                    Properties.Settings.Default.Save();
                    startupLog.AppendLine("Setting was saved.");
                }
                else if (command.ToLower() == ("/RemoveHistory").ToLower())
                {
                    // Remove Run History file.
                    startupLog.AppendLine("The history file will be delete.");
                    if (File.Exists(Util.RunHistoryPath))
                    {
                        try
                        {
                            File.Delete(Util.RunHistoryPath);
                            startupLog.AppendLine("The history file was deleted.");
                        }
                        catch (Exception ex)
                        {
                            startupLog.AppendLine("The history file was not deleted.");
                            MessageBox.Show(ex.Message.ToString(), "Office365APIEditor");
                        }
                    }
                    else
                    {
                        startupLog.AppendLine("The history file does not exist.");
                    }
                }
                else if (command.ToLower() == ("/ResetCustomDefinedScopes").ToLower())
                {
                    // Reset CustomDefinedScopes setting.
                    startupLog.AppendLine("CustomDefinedScopes will be reset.");
                    Properties.Settings.Default.CustomDefinedScopes = null;
                    Properties.Settings.Default.Save();
                    startupLog.AppendLine("CustomDefinedScopes setting was saved.");
                }
                else if (command.ToLower() == ("/EnableSystemLogging").ToLower())
                {
                    // Turn on SystemLogging flag later.
                    systemLogging = true;
                }
            }

            if (systemLogging)
            {
                // Turn on SystemLogging flag.
                startupLog.AppendLine("Enable SystemLogging.");
                Properties.Settings.Default.SystemLogging = true;
                Properties.Settings.Default.Save();
            }

            // Set default log folder path.
            startupLog.AppendLine("Current log folder : " + Properties.Settings.Default.LogFolderPath);
            if (!Directory.Exists(Properties.Settings.Default.LogFolderPath))
            {
                startupLog.AppendLine("The log folder does not exist and will be reset.");
                Properties.Settings.Default.LogFolderPath = Util.DefaultApplicationPath;
                Properties.Settings.Default.Save();
                startupLog.AppendLine("New log folder : " + Properties.Settings.Default.LogFolderPath);
                startupLog.AppendLine("Setting was saved.");
            }

            // Write startup log.
            Util.WriteSystemLog("Office365APIEditor startup log", startupLog.ToString());

            // Create the MyApplicationContext, that derives from ApplicationContext,
            // that manages when the application should exit.

            MyApplicationContext context = new MyApplicationContext();

            // Start checking the latest version, and download the latest installer if it is available
            StartLatestVersionCheck();

            // Run the application with the specific context. It will exit when
            // all forms are closed.
            try
            {
                Application.Run(context);
            }
            catch (Exception ex)
            {
                // Write error log.
                try
                {
                    string filePath = Path.Combine(Util.DefaultApplicationPath, "Error.txt");

                    using (StreamWriter writer = new StreamWriter(filePath, true))
                    {
                        writer.WriteLine(
                            "Date :" + DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss") + Environment.NewLine +
                            "Message :" + ex.Message + Environment.NewLine +
                            "StackTrace :" + ex.StackTrace + Environment.NewLine +
                            "-----------------------------------------------------------------------------" + Environment.NewLine
                            );
                    }
                }
                catch
                {
                }

                if (ex.InnerException == null)
                {
                    MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    MessageBox.Show(ex.InnerException.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            finally
            {
                Util.WriteSystemLog("Office365APIEditor Closing Log", "Exit. Code 10");
            }
        }