// Inserts the collected data to the connected database (helper method)
        private void InsertFeedbackFormItem(FeedbackFormMessageConsentModel feedback)
        {
            // Creates a form item containing the collected data
            var item = new FeedbackFormItemConsent
            {
                UserName     = feedback.FirstName,
                UserLastName = feedback.LastName,
                UserEmail    = feedback.Email,
                UserFeedback = feedback.MessageText,
            };

            // Creates a consent agreement if the consent checkbox was selected
            if (feedback.ConsentIsAgreed)
            {
                // Gets the current contact
                var contact = contactTrackingService.GetCurrentContactAsync(User.Identity.Name).Result;

                // Creates an agreement for the specified consent and contact
                var agreement = formConsentAgreementService.Agree(contact, consent, item);

                // Adds the GUID of the agreement into the form's consent field
                item.ConsentAgreement = agreement.ConsentAgreementGuid;
            }

            // Inserts the form data into the database
            item.Insert();
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Register(RegisterWithConsentViewModel model)
        {
            // Validates the received user data based on the view model
            if (!ModelState.IsValid)
            {
                model.ConsentShortText = consent.GetConsentText("en-US").ShortText;
                return(View("RegisterWithConsent", model));
            }

            // Prepares a new user entity using the posted registration data
            Kentico.Membership.User user = new User
            {
                UserName  = model.UserName,
                Email     = model.Email,
                FirstName = model.FirstName,
                LastName  = model.LastName,
                Enabled   = true // Enables the new user directly
            };

            // Attempts to create the user in the Kentico database
            IdentityResult registerResult = IdentityResult.Failed();

            try
            {
                registerResult = await KenticoUserManager.CreateAsync(user, model.Password);
            }
            catch (Exception ex)
            {
                // Logs an error into the Kentico event log if the creation of the user fails
                eventLogService.LogException("MvcApplication", "UserRegistration", ex);
                ModelState.AddModelError(String.Empty, "Registration failed");
            }

            // If the registration was not successful, displays the registration form with an error message
            if (!registerResult.Succeeded)
            {
                foreach (string error in registerResult.Errors)
                {
                    ModelState.AddModelError(String.Empty, error);
                }

                model.ConsentShortText = consent.GetConsentText("en-US").ShortText;

                return(View("RegisterWithConsent", model));
            }

            // Creates a consent agreement if the consent checkbox was selected in the registration form
            if (model.ConsentIsAgreed)
            {
                // Gets the current contact
                var currentContact = ContactManagementContext.GetCurrentContact();

                // Creates an agreement for the specified consent and contact
                // Passes the UserInfo object of the new user as a parameter, which is used to map the user's values
                // to a new contact in cases where the contact parameter is null,
                // e.g. for visitors who have not given an agreement with the site's tracking consent.
                formConsentAgreementService.Agree(currentContact, consent, userInfoProvider.Get(user.Id));
            }

            // If the registration was successful, signs in the user and redirects to a different action
            await KenticoSignInManager.SignInAsync(user, true, false);

            return(RedirectToAction("Index", "Home"));
        }