public async Task <IActionResult> PutIdentificationValidator(int id, IdentificationValidator identificationValidator) { if (id != identificationValidator.Id) { return(BadRequest()); } _context.Entry(identificationValidator).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!IdentificationValidatorExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <IdentificationValidator> > PostIdentificationValidator(IdentificationValidator identificationValidator) { _context.validators.Add(identificationValidator); await _context.SaveChangesAsync(); return(CreatedAtAction("GetIdentificationValidator", new { id = identificationValidator.Id }, identificationValidator)); }
// Connect user private async void LoginBtn_Click(object sender, EventArgs e) { // Get user data to a model User UserData = new User { Email = EmailTB.Text, Password = PasswordTB.Text }; // Validate Model IdentificationValidator validator = new IdentificationValidator(); ValidationResult result = await validator.ValidateAsync(UserData); if (!result.IsValid) { // Display Errors to user foreach (ValidationFailure failure in result.Errors) { UserErrors.Add(failure.ErrorMessage); } MetroFramework.MetroMessageBox.Show(this, string.Join("\n", UserErrors), "Données Non Valides", MessageBoxButtons.OK, MessageBoxIcon.Error, 400); // Clear errors for another use and quit UserErrors.Clear(); return; } // All data is valid let's check database for the user using (ClubDbContext _context = new ClubDbContext()) { // Get user from db with email try { User userInDb = await _context.Users.SingleOrDefaultAsync(u => u.Email == UserData.Email); // Verify if the email exists and if the password matches the hashed one in Db if (userInDb != null && Crypto.VerifyHashedPassword(userInDb.Password, UserData.Password)) { // Password and email matches let's notify the main form (user connected) OnUserLoggedIn(userInDb); } else { // User data incorrect, display error message MetroFramework.MetroMessageBox.Show(this, "L'email que vous avez rentrez et/ou le mot de passe" + "ne sont pas valide, Veuillez Réssayer!", "Données non valides", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
// Register user private async void RegisterBtn_Click(object sender, EventArgs e) { // Get user data to a model User newUser = new User { Email = EmailTB.Text, Password = PasswordTB.Text }; // Validate Model IdentificationValidator validator = new IdentificationValidator(); ValidationResult result = await validator.ValidateAsync(newUser); // Validate password confirmation if (!ConfirmPasswordTB.Text.Equals(newUser.Password)) { result.Errors.Add(new ValidationFailure("Confirm Password", "Les mots de passe ne sont pas identiques.")); } if (!result.IsValid) { // Display Errors to user foreach (ValidationFailure failure in result.Errors) { UserErrors.Add(failure.ErrorMessage); } MetroFramework.MetroMessageBox.Show(this, string.Join("\n", UserErrors), "Données Non Valides", MessageBoxButtons.OK, MessageBoxIcon.Error); // Clear errors for another use and quit UserErrors.Clear(); return; } // Add Role information to the new user model newUser.Role = User.NonRegisteredClient; // Hash the password for security newUser.Password = Crypto.HashPassword(newUser.Password); // Input data is valid let's add to database using (ClubDbContext _context = new ClubDbContext()) { // check for E-mail already used if (await _context.Users.SingleOrDefaultAsync(u => u.Email == newUser.Email) != null) { MetroFramework.MetroMessageBox.Show(this, "L'email que vous avez rentré est déjà utilisé par quelqu'un d'autre,\n" + "Entrez en un nouveau ou connectez vous!", "E-mail déjà utilisé", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // All is valid now we need to add to db ! _context.Users.Add(newUser); await _context.SaveChangesAsync(); // Registered success, notify the Main form to switch to login OnUserRegistered(); } }