public async Task UpdateUserGroup(string id, string groupName) { var groupsResponse = await provider.AdminListGroupsForUserAsync(new AdminListGroupsForUserRequest { Username = id, UserPoolId = Configurations.Cognito.CognitoPoolId, } ); var userAlreadyInGroup = false; if (groupsResponse.Groups.Count > 0) { foreach (var group in groupsResponse.Groups) { if (group.GroupName == groupName) { userAlreadyInGroup = true; continue; } else { await provider.AdminRemoveUserFromGroupAsync(new AdminRemoveUserFromGroupRequest { Username = id, UserPoolId = Configurations.Cognito.CognitoPoolId, GroupName = group.GroupName, }); } } } if (userAlreadyInGroup) { return; } var request = new AdminAddUserToGroupRequest { Username = id, UserPoolId = Configurations.Cognito.CognitoPoolId, GroupName = groupName, }; await provider.AdminAddUserToGroupAsync(request); }