Exemple #1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        // If StopProcessing flag is set, do nothing
        if (StopProcessing)
        {
            Visible = false;
            return;
        }

        string   subscriptionHash = QueryHelper.GetString("subscriptionhash", string.Empty);
        string   requestTime      = QueryHelper.GetString("datetime", string.Empty);
        DateTime datetime         = DateTimeHelper.ZERO_TIME;

        // Get date and time
        if (!string.IsNullOrEmpty(requestTime))
        {
            try
            {
                datetime = DateTimeUrlFormatter.Parse(requestTime);
            }
            catch
            {
                lblInfo.Text = ResHelper.GetString("newsletter.approval_failed");
                return;
            }
        }

        if (string.IsNullOrEmpty(subscriptionHash))
        {
            Visible = false;
            return;
        }

        var approvalService = Service.Resolve <ISubscriptionApprovalService>();
        var result          = approvalService.ApproveSubscription(subscriptionHash, false, SiteContext.CurrentSiteName, datetime);

        switch (result)
        {
        case ApprovalResult.Success:
            lblInfo.Text = !String.IsNullOrEmpty(SuccessfulApprovalText) ? SuccessfulApprovalText : ResHelper.GetString("newsletter.successful_approval");
            break;

        case ApprovalResult.Failed:
            lblInfo.Text = !String.IsNullOrEmpty(UnsuccessfulApprovalText) ? UnsuccessfulApprovalText : ResHelper.GetString("newsletter.approval_failed");
            break;

        case ApprovalResult.TimeExceeded:
            lblInfo.Text = !String.IsNullOrEmpty(UnsuccessfulApprovalText) ? UnsuccessfulApprovalText : ResHelper.GetString("newsletter.approval_timeexceeded");
            break;

        case ApprovalResult.AlreadyApproved:
            lblInfo.Text = !String.IsNullOrEmpty(SuccessfulApprovalText) ? SuccessfulApprovalText : ResHelper.GetString("newsletter.successful_approval");
            break;

        // Subscription not found
        default:
            lblInfo.Text = !String.IsNullOrEmpty(UnsuccessfulApprovalText) ? UnsuccessfulApprovalText : ResHelper.GetString("newsletter.approval_invalid");
            break;
        }
    }
        //EndDocSection:Unsubscribe


        //DocSection:ConfirmSubscription
        /// <summary>
        /// Handles confirmation requests for newsletter subscriptions (when using double opt-in).
        /// </summary>
        public IActionResult ConfirmSubscription(NewsletterSubscriptionConfirmationViewModel model)
        {
            // Verifies that the confirmation request contains the required hash parameter
            if (!ModelState.IsValid)
            {
                // If the hash is missing, returns a view informing the user that the subscription confirmation was not successful
                ModelState.AddModelError(String.Empty, "The confirmation link is invalid.");
                return(View(model));
            }

            // Attempts to parse the date and time parameter from the request query string
            // Uses the date and time formats required by the Xperience API
            DateTime parsedDateTime = DateTimeHelper.ZERO_TIME;

            if (!string.IsNullOrEmpty(model.DateTime) && !DateTimeUrlFormatter.TryParse(model.DateTime, out parsedDateTime))
            {
                // Returns a view informing the user that the subscription confirmation was not successful
                ModelState.AddModelError(String.Empty, "The confirmation link is invalid.");
                return(View(model));
            }

            // Attempts to confirm the subscription specified by the request's parameters
            ApprovalResult result = subscriptionApprovalService.ApproveSubscription(model.SubscriptionHash, false, siteService.CurrentSite.SiteName, parsedDateTime);

            switch (result)
            {
            // The confirmation was successful or the recipient was already approved
            // Displays a view informing the user that the subscription is active
            case ApprovalResult.Success:
            case ApprovalResult.AlreadyApproved:
                return(View(model));

            // The confirmation link has expired
            // Expiration occurs after a certain number of hours from the time of the original subscription
            // You can set the expiration interval in Xperience (Settings -> On‑line marketing -> Email marketing -> Double opt-in interval)
            case ApprovalResult.TimeExceeded:
                ModelState.AddModelError(String.Empty, "Your confirmation link has expired. Please subscribe to the newsletter again.");
                break;

            // The subscription specified in the request's parameters does not exist
            case ApprovalResult.NotFound:
                ModelState.AddModelError(String.Empty, "The subscription that you are attempting to confirm does not exist.");
                break;

            // The confirmation failed
            default:
                ModelState.AddModelError(String.Empty, "The confirmation of your newsletter subscription did not succeed.");
                break;
            }

            // If the subscription confirmation was not successful, displays a view informing the user
            return(View(model));
        }
        public ActionResult ConfirmSubscription(ConfirmSubscriptionModel model)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(String.Empty, ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionInvalidLink"));

                return(View(model));
            }

            DateTime parsedDateTime = DateTimeHelper.ZERO_TIME;

            // Parse date and time from query string, if present
            if (!string.IsNullOrEmpty(model.DateTime) && !DateTimeUrlFormatter.TryParse(model.DateTime, out parsedDateTime))
            {
                ModelState.AddModelError(String.Empty, ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionInvalidDateTime"));

                return(View(model));
            }

            var result = mSubscriptionApprovalService.ApproveSubscription(model.SubscriptionHash, false, SiteContext.CurrentSiteName, parsedDateTime);

            switch (result)
            {
            case ApprovalResult.Success:
                model.ConfirmationResult = ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionSucceeded");
                break;

            case ApprovalResult.AlreadyApproved:
                model.ConfirmationResult = ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionAlreadyConfirmed");
                break;

            case ApprovalResult.TimeExceeded:
                ModelState.AddModelError(String.Empty, ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionTimeExceeded"));
                break;

            case ApprovalResult.NotFound:
                ModelState.AddModelError(String.Empty, ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionInvalidLink"));
                break;

            default:
                ModelState.AddModelError(String.Empty, ResHelper.GetString("DancingGoatMvc.News.ConfirmSubscriptionFailed"));

                break;
            }

            return(View(model));
        }
Exemple #4
0
        // GET: Subscription/ConfirmSubscription
        public ActionResult ConfirmSubscription(ConfirmSubscriptionModel model)
        {
            if (!ModelState.IsValid)
            {
                AddError("The confirmation link is not valid.");

                return(View(model));
            }

            DateTime parsedDateTime = DateTimeHelper.ZERO_TIME;

            // Parse date and time from query string, if present
            if (!string.IsNullOrEmpty(model.DateTime) && !DateTimeUrlFormatter.TryParse(model.DateTime, out parsedDateTime))
            {
                AddError("The confirmation link is not valid.");

                return(View(model));
            }

            var result = subscriptionApprovalService.ApproveSubscription(model.SubscriptionHash, false, SiteContext.CurrentSiteName, parsedDateTime);

            switch (result)
            {
            case ApprovalResult.Success:
                model.ConfirmationResult = localizer["Your subscription has been confirmed."].Value;
                break;

            case ApprovalResult.AlreadyApproved:
                model.ConfirmationResult = localizer["You are already subscribed."].Value;
                break;

            case ApprovalResult.TimeExceeded:
                AddError("Your subscription confirmation link has expired. Please subscribe again.");
                break;

            case ApprovalResult.NotFound:
                AddError("The confirmation link is not valid.");
                break;

            default:
                AddError("The subscription confirmation has failed. Please try again later.");

                break;
            }

            return(View(model));
        }
    /// <summary>
    /// Check that subscription hash is valid and subscription didn't expire
    /// </summary>
    /// <param name="subscriptionHash">Subscription hash to check</param>
    /// <param name="requestTime">Date time of subscription request</param>
    /// <param name="checkOnly">Indicates if only check will be performed</param>
    private void CheckAndSubscribe(string subscriptionHash, string requestTime, bool checkOnly)
    {
        // Get date and time
        DateTime datetime = DateTimeHelper.ZERO_TIME;

        // Get date and time
        if (!string.IsNullOrEmpty(requestTime))
        {
            try
            {
                datetime = DateTimeUrlFormatter.Parse(requestTime);
            }
            catch
            {
                DisplayError(DataHelper.GetNotEmpty(UnsuccessfulConfirmationText, GetString("general.subscription_failed")));
                return;
            }
        }

        // Initialize opt-in result
        OptInApprovalResultEnum result = OptInApprovalResultEnum.NotFound;

        // Check only data consistency
        if (checkOnly)
        {
            if (SubscriptionObject != null)
            {
                // Validate hash
                result = BoardSubscriptionInfoProvider.ValidateHash(SubscriptionObject, subscriptionHash, SiteContext.CurrentSiteName, datetime);
                if ((result == OptInApprovalResultEnum.Success) && (SubscriptionObject.SubscriptionApproved))
                {
                    result = OptInApprovalResultEnum.NotFound;
                }
            }
        }
        else
        {
            // Try to approve subscription
            result = BoardSubscriptionInfoProvider.ApproveSubscription(SubscriptionObject, subscriptionHash, false, SiteContext.CurrentSiteName, datetime);
        }

        // Process result
        switch (result)
        {
        // Approving subscription was successful
        case OptInApprovalResultEnum.Success:
            if (!checkOnly)
            {
                ShowInfo(DataHelper.GetNotEmpty(SuccessfulConfirmationText, GetString("general.subscription_approval")));
                Service.Resolve <ICurrentContactMergeService>().UpdateCurrentContactEmail(SubscriptionObject.SubscriptionEmail, MembershipContext.AuthenticatedUser);
                BoardSubscriptionInfoProvider.LogSubscriptionActivity(SubscriptionObject, null, QueryHelper.GetInteger("cid", 0), QueryHelper.GetInteger("siteid", 0), QueryHelper.GetText("url", ""), QueryHelper.GetInteger("docid", 0), QueryHelper.GetText("camp", ""), PredefinedActivityType.SUBSCRIPTION_MESSAGE_BOARD, true);
            }
            break;

        // Subscription failed due to parameters
        case OptInApprovalResultEnum.Failed:
            DisplayError(DataHelper.GetNotEmpty(UnsuccessfulConfirmationText, GetString("general.subscription_failed")));
            break;

        case OptInApprovalResultEnum.TimeExceeded:
            BoardSubscriptionInfoProvider.DeleteBoardSubscriptionInfo(SubscriptionObject);
            DisplayError(DataHelper.GetNotEmpty(UnsuccessfulConfirmationText, GetString("general.subscription_timeexceeded")));
            break;

        // Subscription not found
        default:
            DisplayError(DataHelper.GetNotEmpty(UnsuccessfulConfirmationText, GetString("general.subscription_invalid")));
            break;
        }
    }
    /// <summary>
    /// Check that subscription hash is valid and subscription didn't expire
    /// </summary>
    /// <param name="subGuid">Subscription GUID for subscriptions without</param>
    /// <param name="subscriptionHash">Subscription hash to check</param>
    /// <param name="requestTime">Date time of subscription request</param>
    /// <param name="checkOnly">Indicates if only check will be performed</param>
    private void CheckAndUnsubscribe(Guid subGuid, string subscriptionHash, string requestTime, bool checkOnly)
    {
        OptInApprovalResultEnum result = OptInApprovalResultEnum.NotFound;

        // Get date and time
        DateTime datetime = DateTimeHelper.ZERO_TIME;

        if (!String.IsNullOrEmpty(requestTime))
        {
            try
            {
                datetime = DateTimeUrlFormatter.Parse(requestTime);
            }
            catch
            {
                DisplayError(GetString("general.unsubscription_unsuccessful"));
                return;
            }
        }

        if (SubscriptionObject != null)
        {
            if (subGuid != Guid.Empty)
            {
                if (!checkOnly)
                {
                    BlogPostSubscriptionInfoProvider.DeleteBlogPostSubscriptionInfo(SubscriptionObject);
                    result = OptInApprovalResultEnum.Success;
                }
                else
                {
                    // Subscription exists but do nothing automatically, wait to user action
                    return;
                }
            }
            // Check if subscription approval hash is supplied
            else if (!string.IsNullOrEmpty(subscriptionHash))
            {
                result = checkOnly
                    ? BlogPostSubscriptionInfoProvider.ValidateHash(SubscriptionObject, subscriptionHash, SiteContext.CurrentSiteName, datetime)
                    : BlogPostSubscriptionInfoProvider.Unsubscribe(subscriptionHash, true, SiteContext.CurrentSiteName, datetime);
            }
        }

        switch (result)
        {
        // Approving subscription was successful
        case OptInApprovalResultEnum.Success:
            if (!checkOnly)
            {
                ShowInfo(DataHelper.GetNotEmpty(SuccessfulUnsubscriptionText, GetString("Unsubscribe.Unsubscribed")));
            }
            break;

        // Subscription was already approved
        case OptInApprovalResultEnum.Failed:
            DisplayError(DataHelper.GetNotEmpty(UnsuccessfulUnsubscriptionText, GetString("general.unsubscription_unsuccessful")));
            break;

        case OptInApprovalResultEnum.TimeExceeded:
            DisplayError(DataHelper.GetNotEmpty(UnsuccessfulUnsubscriptionText, GetString("general.unsubscription_timeexceeded")));
            break;

        // Subscription not found
        default:
            DisplayError(DataHelper.GetNotEmpty(UnsuccessfulUnsubscriptionText, GetString("general.unsubscription_NotSubscribed")));
            break;
        }
    }