public async Task <IActionResult> Login([FromBody] LogInForm model) { if (!ModelState.IsValid || model == null) { return(BadRequest(ModelState)); } var user = await _db.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Email.Equals(model.Email, StringComparison.OrdinalIgnoreCase)); if (user == null) { return(BadRequest(ControllerErrorCode.AccountOrPasswordWrong)); } var salt = user.Salt; var passHash = user.PassHash; var cryptoProvider = new CryptographyProcessor(); if (!cryptoProvider.AreEqual(model.Password, passHash, salt)) { return(BadRequest(ControllerErrorCode.AccountOrPasswordWrong)); } if (!user.IsConfirmed) { return(BadRequest(ControllerErrorCode.NotConfirmed)); } var token = await _token.GetTokenAsync(user); return(Ok(token)); }
private void buttonLogin_Click(object sender, EventArgs e) { if (errorProvider1.GetError(loginInput) == "" && errorProvider1.GetError(passwordInput) == "" && loginInput.Text.Trim() != "" && passwordInput.Text.Trim() != "") { Employee currentUser = hospitalStructure.Employees. Where(user => user.Login == loginInput.Text). FirstOrDefault(); if (currentUser == null) { MessageBox.Show( "No user found with given login.", "Something went wrong...", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (currentUser.IsBanned()) { MessageBox.Show( "You are banned!", "Something went wrong...", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (CryptographyProcessor.AreEqual(passwordInput.Text, currentUser.Password, currentUser.Salt)) { var t = new Thread(() => Application.Run(new Main(currentUser, hospitalStructure))); t.Start(); Thread.Sleep(50); this.Close(); } else { MessageBox.Show( "Password is wrong! Please try again!", "Something went wrong...", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Check user method to verify if such a user exists /// </summary> /// <param name="model">User login model</param> /// <returns>true/false if user found/not found</returns> public async Task <bool> CheckUser(UserLoginModel model) { var user = await _unitOfWork.UserRepository.FindOneAsync(u => u.Email == model.Email); if (user == null) { throw new ArgumentException($"User with Email: {model.Email} doesn't exists"); } if (!CryptographyProcessor.AreEqual(model.Password, user.PasswordHash, user.Salt)) { throw new ArgumentException("You have entered an invalid password"); } return(true); }