public async Task <Guid> DeployAndSetSampleProject(string token, UserModel user, IEnumerable <SubscriptionModel> subscriptions = null)
        {
            var administeredSubscriptionId = user.AdministeredSubscriptions.FirstOrDefault()?.SubscriptionId;
            var actualSubscriptions        = subscriptions ?? await _subscriptionProvider.GetSubscriptionsAsync(token);

            var administeredSubscription = actualSubscriptions.FirstOrDefault(s => s.SubscriptionId == administeredSubscriptionId);

            if (administeredSubscriptionId.HasValue && administeredSubscription != null)
            {
                var project = await _projectProvider.DeploySampleAsync(token, administeredSubscriptionId.Value);

                SetProjectIdAndExpirationAsync(project.ProjectId.Value, administeredSubscription.CurrentPlan?.EndAt);
                await _projectProvider.RenameProjectAsync(token, project.ProjectId.Value);

                return(project.ProjectId.Value);
            }
            else
            {
                throw new Exception("There was no subscription found to deploy the sample project to.");
            }
        }
        public async Task <ActionResult> Index(string token)
        {
            string actualToken = token ?? GetToken();

            if (!string.IsNullOrEmpty(actualToken))
            {
                try
                {
                    if (!string.IsNullOrEmpty(token))
                    {
                        AddAuthenticationCookie(token);
                    }

                    var user = await _userProvider.GetUserAsync(actualToken);

                    IEnumerable <SubscriptionModel> subscriptions;

                    if (user != null)
                    {
                        subscriptions = await _subscriptionProvider.GetSubscriptionsAsync(actualToken);
                    }
                    else
                    {
                        return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                            Caption = null, Message = MESSAGE_UNAUTHENTICATED, MessageType = MessageType.Error
                        }));
                    }

                    if (subscriptions == null || !subscriptions.Any())
                    {
                        SubscriptionModel subscription = await _subscriptionProvider.StartTrial(actualToken);

                        ProjectModel project = null;

                        try
                        {
                            if (subscription != null)
                            {
                                project = await _projectProvider.DeploySampleAsync(token, subscription.SubscriptionId);
                            }
                        }
                        catch (DeliveryException)
                        {
                            return(UseSharedPrivate());
                        }

                        if (subscription != null && project != null)
                        {
                            try
                            {
                                _selfConfigManager.SetProjectIdAndExpirationAsync(project.ProjectId.Value, subscription.EndAt);
                                await _projectProvider.RenameProjectAsync(token, project.ProjectId.Value);

                                return(RedirectHelpers.GetHomeRedirectResult(new MessageModel {
                                    Caption = null, Message = string.Format(MESSAGE_NEW_SAMPLE_PROJECT, project.ProjectId.Value), MessageType = MessageType.Info
                                }));
                            }
                            catch (ConfigurationErrorsException ex)
                            {
                                return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                                    Caption = null, Message = ex.Message, MessageType = MessageType.Error
                                }));
                            }
                        }
                        else
                        {
                            try
                            {
                                _selfConfigManager.SetSharedProjectIdAsync();

                                return(RedirectHelpers.GetHomeRedirectResult(new MessageModel {
                                    Caption = null, Message = MESSAGE_SHARED_PROJECT, MessageType = MessageType.Info
                                }));
                            }
                            catch (ConfigurationErrorsException ex)
                            {
                                return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                                    Caption = null, Message = ex.Message, MessageType = MessageType.Error
                                }));
                            }
                        }
                    }
                    else
                    {
                        SubscriptionStatusResults results = await _subscriptionProvider.GetSubscriptionsStatusAsync(actualToken, subscriptions);

                        if (results.Status == SubscriptionStatus.Active)
                        {
                            if (results.Projects.Any(p => p.Inactive == false))
                            {
                                AddSecurityInfoToViewBag();

                                return(View("SelectOrCreateProject", new SelectProjectViewModel {
                                    Projects = results.Projects.Where(p => p.Inactive == false)
                                }));
                            }
                            else
                            {
                                try
                                {
                                    var projectId = await _selfConfigManager.DeployAndSetSampleProject(actualToken, user);

                                    return(RedirectHelpers.GetHomeRedirectResult(new MessageModel {
                                        Caption = null, Message = string.Format(MESSAGE_NEW_SAMPLE_PROJECT, projectId), MessageType = MessageType.Info
                                    }));
                                }
                                catch (ConfigurationErrorsException ex)
                                {
                                    return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                                        Caption = null, Message = ex.Message, MessageType = MessageType.Error
                                    }));
                                }
                                catch (DeliveryException ex)
                                {
                                    return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                                        Caption = null, Message = ex.Message, MessageType = MessageType.Error
                                    }));
                                }
                            }
                        }
                        else
                        {
                            ViewBag.EndAt = results.EndAt;

                            return(View(results.Status.ToString(), new SelectProjectViewModel {
                                Projects = results.Projects.Where(p => p.Inactive == false)
                            }));
                        }
                    }
                }
                catch (JsonException ex)
                {
                    return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                        Caption = null, Message = $"{CAPTION_DESERIALIZATION_ERROR}: {ex.Message}", MessageType = MessageType.Error
                    }));
                }
            }
            else
            {
                return(RedirectHelpers.GetSelfConfigIndexResult(new MessageModel {
                    Caption = null, Message = MESSAGE_UNAUTHENTICATED, MessageType = MessageType.Error
                }));
            }
        }