private ActionResult SetConfiguration(string message, Guid projectGuid, DateTime?endAt, bool isNew)
        {
            try
            {
                _selfConfigManager.SetProjectIdAndExpirationAsync(projectGuid, endAt?.ToUniversalTime());

                if (isNew)
                {
                    return(View("Wait"));
                }

                return(RedirectHelpers.GetHomeRedirectResult(new MessageModel {
                    Caption = null, Message = message, MessageType = MessageType.Info
                }));
            }
            catch (ConfigurationErrorsException ex)
            {
                return(View("Error", new MessageModel {
                    Caption = CAPTION_CONFIGURATION_WRITE_ERROR, Message = ex.Message, MessageType = MessageType.Error
                }));
            }
            catch (JsonSerializationException ex)
            {
                return(View("Error", new MessageModel {
                    Caption = CAPTION_DESERIALIZATION_ERROR, Message = ex.Message, MessageType = MessageType.Error
                }));
            }
        }
        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
                }));
            }
        }