/// <summary> /// Send the old and new password to the server, if the password are good, /// the password for the user is changed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void OnButtonChangePasswordPressed(object sender, EventArgs e) { IndicatorOn(); WrongCompleted = false; NotIdentic = false; WrongPassword = false; Succeeded = false; ErrorRequest = false; ErrorConflict = false; String oldPassword = OldPasswordEntry.Text; String newPassord = NewPasswordEntry.Text; String confirmedPassword = ConfirmPasswordEntry.Text; await Task.Run(async() => { if (CrossConnectivity.Current.IsConnected) { if (oldPassword == null || newPassord == null || confirmedPassword == null || oldPassword.Equals("") || newPassord.Equals("") || confirmedPassword.Equals("")) { WrongCompleted = true; return; } if (!newPassord.Equals(confirmedPassword)) { NotIdentic = true; return; } if (!CreateAccountManager.CheckPassword(newPassord)) { WrongPassword = true; return; } //hash password SHA1Managed sha1 = new SHA1Managed(); var oldHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(oldPassword)); String oldPasswordHashed = String.Join("", oldHash.Select(b => b.ToString("x2")).ToArray()); var newHash = sha1.ComputeHash(Encoding.UTF8.GetBytes(newPassord)); String newPasswordHashed = String.Join("", newHash.Select(b => b.ToString("x2")).ToArray()); //Create json String jsonModifyPassword = "******"Login\":\"" + currentUser.Login + "\", \"Old_password\":\"" + oldPasswordHashed + "\", \"New_password\":\"" + newPasswordHashed + "\"}"; //Send json try { HttpWebRequest webRequest = WebRequest.Create(ApplicationConstants.serverChangePasswordURL) as HttpWebRequest; webRequest.ContentType = "application/json"; webRequest.Method = "POST"; JsonParser.SendToServer(webRequest, jsonModifyPassword); HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse; //If response OK, quit if (webResponse.StatusCode.Equals(HttpStatusCode.OK)) { Succeeded = true; return; } } catch (Exception exception) { WebException webException = exception as WebException; HttpWebResponse response = webException.Response as HttpWebResponse; //If conflict, warn user if (response.StatusCode.Equals(HttpStatusCode.Conflict)) { ErrorConflict = true; return; } else { ErrorRequest = true; return; } } } else { await DisplayAlert("Erreur", "Vous devez être connecter à internet pour modifier votre mot de passe", "OK"); return; } }); if (Succeeded) { await DisplayAlert("Changer de mot de passe", "Votre mot de passe a bien été modifié", "OK"); await Navigation.PopAsync(); IndicatorOff(); } if (WrongCompleted) { await DisplayAlert("Erreur", "Tous les champs doivent être renseignés", "OK"); } if (WrongPassword) { await DisplayAlert("Erreur", "Le nouveau mot de passe doit contenir au moins 6 caractères dont au moins une majuscule", "OK"); } if (ErrorConflict) { await DisplayAlert("Erreur", "Le changement de mot de passe n'a pas pu être effectué", "OK"); } if (ErrorRequest) { await DisplayAlert("Erreur", "Erreur lors de l'envoi ou du traitement de la requête", "OK"); } if (NotIdentic) { await DisplayAlert("Erreur", "Le nouveau mot de passe et la confirmation doivent être identique", "OK"); } IndicatorOff(); }