public IActionResult Post([FromBody] User user) { if (string.IsNullOrEmpty(user.Password) || string.IsNullOrEmpty(user.Username)) { return(StatusCode(500, "User cannot have empty password or username")); } if (user.Password.Length < 6) { return(StatusCode(500, "User password need to have min 6 signs")); } var salt = HashPassword.GenerateSalt(); var password = HashPassword.GeneratePassword(salt, user.Password); user.Password = password; user.Salt = salt; var existingUser = Users.FirstOrDefault(x => x.Username == user.Username || x.Email == user.Email); if (existingUser != null) { return(StatusCode(500, "Already exist user with that username or email")); } _db.Users.Add(user); _db.SaveChanges(); return(Ok()); }
public void Handle(RegisterUserCommand command) { command.User.Salt = HashPassword.GenerateSalt(50); command.User.Password = HashPassword.GenerateHash(command.User.Password, command.User.Salt); using (_unitOfWork = new UnitOfWork()) { _handler.Handle(command); } }
public IHttpActionResult Register(User user) { if (serviceContext.users.FirstOrDefault(userRegistered => userRegistered.Gebruikersnaam == user.Gebruikersnaam) != null) { return(BadRequest(ModelState)); } user.Salt = HashPassword.GenerateSalt(); user.Wachtwoord = HashPassword.Hashpassword(user.Wachtwoord + user.Salt); serviceContext.users.Add(user); serviceContext.SaveChanges(); return(Ok()); }
public IActionResult Put([FromBody] User user) { var u = Users.FirstOrDefault(x => x.Id == user.Id); byte[] salt = null; if (string.IsNullOrEmpty(user.Password)) { user.Password = u.Password; salt = u.Salt; } else { salt = HashPassword.GenerateSalt(); } if (user.Password.Length < 6) { return(StatusCode(500, "User password need to have min 6 signs")); } var newPassword = HashPassword.GeneratePassword(salt, user.Password); user.Password = newPassword; user.Salt = salt; var existingUser = Users.FirstOrDefault(x => (x.Username == user.Username || x.Email == user.Email) && x.Id != user.Id); if (existingUser != null) { return(StatusCode(500, "Already exist user with that username or email")); } u.Password = newPassword; u.Salt = salt; u.Email = user.Email; u.FirstName = user.FirstName; u.LastName = user.LastName; u.Username = user.Username; _db.Users.Update(u); _db.SaveChanges(); return(Ok()); }