public async Task <IActionResult> CreateUserAsync([FromBody] UserDto userDto) { try { if (string.IsNullOrEmpty(userDto.Password)) { return(this.SetError($"The parameter [{nameof(userDto.Password)}] is required !!", "NullParameter", StatusCodes.Status400BadRequest)); } var user = userDto.ConvertFromDto(); var result = await this.userManager.CreateAsync(user, userDto.Password); if (!result.Succeeded) { var error = result.Errors.FirstOrDefault(); return(this.SetError(error.Description, error.Code, StatusCodes.Status500InternalServerError)); } foreach (var item in userDto.Roles) { if (!await this.rolesManager.RoleExistsAsync(item)) { return(this.SetError($"The Role [{item}] doesn't exists!", "RoleNotFound", StatusCodes.Status400BadRequest)); } var roleResult = await this.userManager.AddToRoleAsync(user, item); if (!roleResult.Succeeded) { var error = roleResult.Errors.FirstOrDefault(); return(this.SetError(error.Description, error.Code, StatusCodes.Status500InternalServerError)); } } return(this.StatusCode(StatusCodes.Status200OK, user.ConvertToDto())); } catch (Exception ex) { return(this.SetError(ex.Message)); } }