Пример #1
0
        /// <summary>
        /// CreateNewPasswordFileButton button was selected.
        /// </summary>
        private async void CreateNewPasswordFileButton_Click(object sender, RoutedEventArgs e)
        {
            _ = sender;
            _ = e;

            ResourceLoader r = ResourceLoader.GetForCurrentView();

            if (await AppData.IsInitialized().ConfigureAwait(true))
            {
                // Clear existing account information and set new password

                string titleStr = null;
                if (null != r)
                {
                    titleStr = r.GetString("INIT_CONFIRM_MSG_TITLE");
                }
                if (null == titleStr)
                {
                    titleStr = "Confirmation";
                }

                string msgStr = null;
                if (null != r)
                {
                    msgStr = r.GetString("INIT_CONFIRM_MSG");
                }
                if (null == msgStr)
                {
                    msgStr = "All registered data will be deleted, including backup. Do you want to run it?";
                }

                MessageDialog msgDlg      = new MessageDialog(msgStr, titleStr);
                string        okLabel     = GlbFunc.GetResourceString("CONFIRM_INITIALIZE_OK_MSG", "OK");
                string        cancelLabel = GlbFunc.GetResourceString("CONFIRM_INITIALIZE_CANCEL_MSG", "Cancel");
                msgDlg.Commands.Add(new UICommand(okLabel));
                msgDlg.Commands.Add(new UICommand(cancelLabel));
                var result = await msgDlg.ShowAsync();

                if (result.Label != okLabel)
                {
                    return;
                }

                // Get the password of the new file.
                SetNewPassDialog newPassDlg = new SetNewPassDialog(false, false);
                await newPassDlg.ShowAsync();

                if (!newPassDlg.IsOK)
                {
                    return;
                }
                AppData.Initialize(newPassDlg.Password, newPassDlg.SavePassword);
            }
            else
            {
                // Specify new password and initialize data structure.

                // Get the password of the new file.
                SetNewPassDialog d = new SetNewPassDialog(false, false);
                await d.ShowAsync();

                if (!d.IsOK)
                {
                    return;
                }
                AppData.Initialize(d.Password, d.SavePassword);
            }
            await UpdateWindowStat().ConfigureAwait(true);
        }
Пример #2
0
        // Load password file
        private static async Task <Tuple <PasswordFile, string> > LoadSelectedPasswordFile()
        {
            // Get stored password
            string savedPass  = AppData.GetSavedUserPassword();
            string masterPass = "";
            bool   passerror;
            bool   isFirst = true;

            if (!string.IsNullOrEmpty(savedPass))
            {
                if (!AppData.Authenticate(savedPass, out passerror, out masterPass))
                {
                    if (!passerror)
                    {
                        // Unexpected error
                        return(new Tuple <PasswordFile, string>(null, ""));
                    }
                    masterPass = "";
                    isFirst    = false;
                }
            }

            string d_userPass    = "";
            bool   d_savePassFlg = false;

            while (string.IsNullOrEmpty(masterPass))
            {
                // Show password dialog
                PasswordDialog d = new PasswordDialog();
                d.Password     = d_userPass;
                d.SavePassword = d_savePassFlg;
                d.IsFirstTime  = isFirst;
                await d.ShowAsync();

                // If canceled, failed to load file
                if (!d.IsOK)
                {
                    return(new Tuple <PasswordFile, string>(null, ""));
                }
                d_userPass    = d.Password;
                d_savePassFlg = d.SavePassword;

                if (!AppData.Authenticate(d_userPass, out passerror, out masterPass))
                {
                    if (!passerror)
                    {
                        // Unexpected error
                        return(new Tuple <PasswordFile, string>(null, ""));
                    }
                    // Retry
                    masterPass = "";
                    isFirst    = false;
                }
            }

            // If authenticated and save password flag is specified, save inputed password string
            if (d_savePassFlg)
            {
                AppData.SaveUserPassword(d_userPass);
            }

            PasswordFile pwFile     = new PasswordFile();
            bool         loadResult = pwFile.Load(masterPass);

            if (!loadResult)
            {
                // Unexpected error
                return(new Tuple <PasswordFile, string>(null, ""));
            }

            return(new Tuple <PasswordFile, string>(pwFile, masterPass));
        }