/// <summary> /// Create a jury account by sending account information to the server /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void OnButtonInscriptionClicked(object sender, EventArgs e) { IndicatorOn(); string loginText = LoginEntry.Text; string passwordText = PasswordEntry.Text; string mailText = MailEntry.Text; string nomText = NomEntry.Text; string prenomText = PrenomEntry.Text; String errorMessage = ""; Error = false; Succeed = false; WrongPassword = false; WrongMail = false; WrongCompleted = false; StartAnonym = false; await Task.Run(async() => { CreateAccountManager accountManager = new CreateAccountManager(); if (loginText == null || nomText == null || prenomText == null || mailText == null || passwordText == null || loginText.Equals("") || nomText.Equals("") || prenomText.Equals("") || mailText.Equals("") || passwordText.Equals("")) { WrongCompleted = true; return; } if (loginText.ToLower().StartsWith("jury")) { StartAnonym = true; return; } if (CreateAccountManager.CheckPassword(PasswordEntry.Text) == false) { WrongPassword = true; return; } if (CreateAccountManager.CheckMail(MailEntry.Text) == false) { WrongMail = true; return; } accountManager.Login = LoginEntry.Text; accountManager.Lastname = NomEntry.Text; accountManager.Firstname = PrenomEntry.Text; accountManager.Mail = MailEntry.Text; //Compute password hash (SHA1) SHA1Managed sha1 = new SHA1Managed(); var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(PasswordEntry.Text)); String hashedPassword = String.Join("", hash.Select(b => b.ToString("x2")).ToArray()); accountManager.Password = hashedPassword; //Create json String createAccountJson = JsonConvert.SerializeObject(accountManager); //Send json try { HttpWebRequest webRequest = WebRequest.Create(ApplicationConstants.serverCreateAccountURL) as HttpWebRequest; webRequest.ContentType = "application/json"; webRequest.Method = "POST"; JsonParser.SendToServer(webRequest, createAccountJson); HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse; //If response OK, quit if (webResponse.StatusCode.Equals(HttpStatusCode.OK)) { Succeed = 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)) { StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); errorMessage = reader.ReadToEnd(); Error = true; return; } } }); if (Error) { await DisplayAlert("Erreur", errorMessage, "OK"); } if (Succeed) { await DisplayAlert("Création de compte", "Votre compte a bien été créé", "OK"); IndicatorOff(); await Navigation.PopAsync(); } if (WrongCompleted) { await DisplayAlert("Erreur", "Tous les champs doivent être renseignés", "OK"); } if (WrongMail) { await DisplayAlert("Erreur", "Adresse mail invalide", "OK"); } if (WrongPassword) { await DisplayAlert("Erreur", "Le mot de passe doit contenir au moins 6 caractères dont au moins une lettre en majuscule", "OK"); } if (StartAnonym) { await DisplayAlert("Erreur", "Le login ne peut pas commencer par 'Jury'", "OK"); } IndicatorOff(); }
/// <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(); }