private async void ChangePasswordButton_Click(object sender, RoutedEventArgs e) { _ = sender; _ = e; // Show password dialog at first time. SetNewPassDialog d = new SetNewPassDialog(true, false); d.Password = ""; await d.ShowAsync(); // If canceled, give up to change password. if (!d.IsOK) { return; } string currentPass = d.OldPassword; string newPass = d.Password; bool saveFlg = d.SavePassword; // Try to change password, and if failed, request new password again. while (!AppData.ChangeUserPassword(currentPass, newPass)) { // Show dialog d = new SetNewPassDialog(true, true); d.Password = newPass; d.SavePassword = saveFlg; await d.ShowAsync(); // If canceled, give up to change password. if (!d.IsOK) { return; } currentPass = d.OldPassword; newPass = d.Password; saveFlg = d.SavePassword; } if (saveFlg) { // If save password flag is set, save new password string. AppData.SaveUserPassword(newPass); } else { // If save password flag is not set, clear saved old password. AppData.ClearSavedUserPassword(); } }
private async Task <bool> LoadPasswordFile() { string masterPass = ""; bool passerror; bool isFirst = true; // Get saved password string savedPass = AppData.GetSavedUserPassword(); if (!string.IsNullOrEmpty(savedPass)) { if (!AppData.Authenticate(savedPass, out passerror, out masterPass)) { // If authenticate is failed by unknown error, it failed to load file. if (!passerror) { return(false); } 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(false); } d_userPass = d.Password; d_savePassFlg = d.SavePassword; if (!AppData.Authenticate(d_userPass, out passerror, out masterPass)) { // If authenticate is failed by unknown error, it failed to load file. if (!passerror) { return(false); } masterPass = ""; isFirst = false; } } // If authenticated and save password flag is specified, save inputed password string if (d_savePassFlg) { AppData.SaveUserPassword(d_userPass); } // Load password file PasswordFile pf = new PasswordFile(); if (pf.Load(masterPass)) { // hold opened password file m_PasswordFile = pf; } else { // If failed to file, create new empty file. m_PasswordFile = new PasswordFile(); } // hold master password string for future saving m_Password = masterPass; m_LastEditDate = DateTime.UtcNow; EditEnableToggle.OnContent = SAVED_LABEL_TEXT; return(true); }
// 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)); }