public async Task <UserResponse> AddUserAsync(UserRequest request) { try { //check user eamil availability var userEmailAvailability = _dbContext.User.Any(p => p.Email.ToLower() == request.Email.ToLower()); if (userEmailAvailability) { throw new Exception("Email Id already exists"); } var invitation = await _azureAdService.InviteUserAsync(request.Email, $"{request.LastName}, {request.FirstName}").ConfigureAwait(false); if (invitation != null) { var user = new User(); user.FirstName = request.FirstName; user.LastName = request.LastName; user.Email = request.Email; user.Mobile = request.Mobile; user.IsActive = true; user.UserType = string.IsNullOrEmpty(request.UserType) ? UserType.Dkbs.ToString() : request.UserType; user.CreatedBy = request.UserName; user.CreatedDate = DateTime.UtcNow; user.LastModified = DateTime.UtcNow; user.LastModifiedBy = request.UserName; user.AzureAdId = Guid.Parse(invitation.InvitedUser.Id); if (request.RoleIds != null) { foreach (var roleId in request.RoleIds) { var userRole = new UserRole() { RoleId = roleId, UserId = user.UserId }; user.UserRoles.Add(userRole); } } _dbContext.User.Add(user); await _dbContext.SaveChangesAsync(); return(_mapper.Map <UserResponse>(user)); } } catch (Exception) { throw; } return(null); }