public IActionResult Post(DtoUserRegister dto) { if (_db.Users.UserExists(dto.Username)) { ModelState.AddModelError("Username", "This username is already taken"); } if (dto.Password != dto.PasswordConfirm) { ModelState.AddModelError("PasswordConfirm", "The password and the confirmation password do not match"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new ChirpUser { Username = dto.Username, Password = _passwordHasher.HashPassword(dto.Password) }; _db.Users.Add(user); _db.SaveChanges(); return(Ok(user.Id)); }
/// <summary> /// Gelen Username ve Emailin Database'de olup olamdığı kontrol eder. /// </summary> /// <param name="users">Username ve Email</param> /// <returns>UserId</returns> public async Task <int> CheckUsernameEmail(DtoUserRegister users) { var query = await(from user in _context.Users where (user.Username == users.Username || user.Email == users.Email) select user.Id).FirstOrDefaultAsync(); return(query); }
public async Task <ActionResult> RegisterUser([FromBody] DtoUserRegister model) { if (model.Password != model.ConfirmPassword) { return(BadRequest(new UserResponse { Message = "Password and confirm password does not match.", IsSuccess = false })); } var user = new IdentityUser { UserName = model.Username }; var checkRoleExist = await _roleManager.RoleExistsAsync(model.Name); if (checkRoleExist == false) { return(BadRequest(new UserResponse { Message = "A role does not exist so it cannot create a user without a role", IsSuccess = false })); } var checkIsInRole = await _userManager.IsInRoleAsync(user, model.Role); if (checkIsInRole) { return(Conflict("User already exists in this role")); } var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var addUserToRole = await _userManager.AddToRoleAsync(user, model.Role); if (addUserToRole.Succeeded) { return(Ok(new UserResponse { Message = $"Successfully created user {model.Username} and successfully added to role {model.Role}", IsSuccess = true, })); } } return(BadRequest()); }
/// <summary> /// Gelen Username ve Emailin Database'de olup olamdığı kontrol eder. /// </summary> /// <param name="users">Username ve Email</param> /// <returns>UserId</returns> public async Task <int> CheckUsernameEmail(DtoUserRegister user) { return(await _userRepository.CheckUsernameEmail(user)); }