/// <summary>
        /// Executes checks which need to run for every request e.g. notifyLogin if CMS is running the session and extending the session if Gigya is managing it.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="settings"></param>
        public static void ProcessRequestChecks(HttpContext context, IGigyaModuleSettings settings = null)
        {
            var currentNode = SiteMapBase.GetCurrentNode();

            if (currentNode != null && currentNode.IsBackend)
            {
                return;
            }

            // don't need to do anything if the user is editing content in the CMS
            var identity = ClaimsManager.GetCurrentIdentity();

            if (identity.IsBackendUser)
            {
                return;
            }

            if (context.Items.Contains(_executedProcessRequestKey))
            {
                return;
            }

            context.Items[_executedProcessRequestKey] = true;

            var settingsHelper   = new GigyaSettingsHelper();
            var logger           = LoggerFactory.Instance();
            var membershipHelper = new GigyaMembershipHelper(new GigyaApiHelper(settingsHelper, logger), logger);
            var accountHelper    = new GigyaAccountHelper(settingsHelper, logger, membershipHelper, settings);

            accountHelper.LoginToGigyaIfRequired();
            accountHelper.UpdateSessionExpirationCookieIfRequired(context);
        }
        public static void ValidateAndLoginToGigyaIfRequired(HttpContext context, IGigyaModuleSettings settings = null)
        {
            if (context.Items.Contains(_checkedIfLoginRequiredKey))
            {
                return;
            }

            context.Items[_checkedIfLoginRequiredKey] = true;

            var settingsHelper   = new GigyaSettingsHelper();
            var logger           = LoggerFactory.Instance();
            var membershipHelper = new GigyaMembershipHelper(new GigyaApiHelper(settingsHelper, logger), logger);
            var accountHelper    = new GigyaAccountHelper(settingsHelper, logger, membershipHelper, settings);

            accountHelper.LoginToGigyaIfRequired();
        }
        /// <summary>
        /// Login or register a user.
        /// </summary>
        /// <param name="model">Details from the client e.g. signature and userId.</param>
        /// <param name="settings">Gigya module settings.</param>
        /// <param name="response">Response model that will be returned to the client.</param>
        public virtual void LoginOrRegister(LoginModel model, IGigyaModuleSettings settings, ref LoginResponseModel response)
        {
            response.Status = ResponseStatus.Error;

            if (!settings.EnableRaas)
            {
                if (settings.DebugMode)
                {
                    _logger.Debug("RaaS not enabled so login aborted.");
                }
                return;
            }

            if (!_gigyaApiHelper.ValidateApplicationKeySignature(model.UserId, settings, model.SignatureTimestamp, model.Signature))
            {
                if (settings.DebugMode)
                {
                    _logger.Debug("Invalid user signature for login request.");
                }
                return;
            }

            // get user info
            var userInfoResponse = _gigyaApiHelper.GetAccountInfo(model.UserId, settings);

            if (userInfoResponse == null || userInfoResponse.GetErrorCode() != 0)
            {
                _logger.Error("Failed to getAccountInfo");
                return;
            }

            List <MappingField> mappingFields = GetMappingFields(settings);
            var gigyaModel = GetAccountInfo(model.Id, settings, userInfoResponse, mappingFields);

            ThrowTestingExceptionIfRequired(settings, gigyaModel);

            // find what field has been configured for the CMS username
            var username = GetCmsUsername(mappingFields, gigyaModel);

            UserManager manager    = UserManager.GetManager();
            var         userExists = manager.UserExists(username);

            if (!userExists)
            {
                // user doesn't exist so create a new one
                using (new ElevatedModeRegion(manager))
                {
                    var createUserStatus = CreateUser(username, gigyaModel, settings, mappingFields);
                    if (createUserStatus != MembershipCreateStatus.Success)
                    {
                        return;
                    }
                }
            }

            // user logged into Gigya so now needs to be logged into Sitefinity
            var authenticated = AuthenticateUser(username, settings, userExists, gigyaModel, mappingFields);

            response.Status = authenticated ? ResponseStatus.Success : ResponseStatus.Error;
            if (authenticated)
            {
                response.RedirectUrl = settings.RedirectUrl;

                var settingsHelper = new GigyaSettingsHelper();
                var accountHelper  = new GigyaAccountHelper(settingsHelper, _logger, this, settings);
                accountHelper.UpdateSessionExpirationCookieIfRequired(HttpContext.Current, true);
            }
        }