public IActionResult RegisterNewUser([FromBody] UserAndPassDto usernameAndPasswordDto) { var user = _macrosRepository.GetUserByUsername(usernameAndPasswordDto.Username); if (user != null) { return(NotFound(new { message = "Username already taken" })); } var salt = Convert.ToBase64String(Common.GetRandomSalt(16)); var finalUserDto = new UserDto() { UId = Guid.NewGuid(), Username = usernameAndPasswordDto.Username, Salt = salt, Password = Convert.ToBase64String(Common.SaltHashPassword(Encoding.ASCII.GetBytes(usernameAndPasswordDto.Password), Convert.FromBase64String(salt))) }; var finalUser = _mapper.Map <Entities.Users>(finalUserDto); _macrosRepository.RegisterUser(finalUser); _macrosRepository.Save(); return(Ok(new { message = "Register Succesfully" })); }
public IActionResult LoginUser([FromBody] UserAndPassDto usernameAndPasswordDto) { var user = _macrosRepository.GetUserByUsername(usernameAndPasswordDto.Username); if (user == null) { return(NotFound("The username does not exist")); } var user_post_hash_password = Convert.ToBase64String( Common.SaltHashPassword(Encoding.ASCII.GetBytes(usernameAndPasswordDto.Password), Convert.FromBase64String(user.Salt))); if (user_post_hash_password.Equals(user.Password)) { return(Ok(user.Uid)); } else { return(NotFound("Incorrect Password")); } }