public async Task <IActionResult> CreateUser(CreateUserRequest request) { var result = new CreateUserRequestValidation().Validate(request); if (!result.IsValid) { foreach (var failure in result.Errors) { ModelState.AddModelError(failure.PropertyName, failure.ErrorMessage); } var errors = ModelState.Values.SelectMany(v => v.Errors.Select(b => b.ErrorMessage)).ToList(); _telemetryClient.TrackTrace(new TraceTelemetry( $"CreateUserRequest validation failed: {string.Join(Separator, errors)}", SeverityLevel.Error)); return(BadRequest(ModelState)); } try { var adUserAccount = await _userAccountService.CreateUserAsync(request.FirstName, request.LastName, request.RecoveryEmail, request.IsTestUser); var response = new NewUserResponse { UserId = adUserAccount.UserId, Username = adUserAccount.Username, OneTimePassword = adUserAccount.OneTimePassword }; return(CreatedAtRoute("GetUserByAdUserId", new { userId = adUserAccount.UserId }, response)); } catch (UserExistsException e) { return(new ConflictObjectResult(new { Message = "User already exists", Code = "UserExists", e.Username })); } }