public async Task <ActionResult> SignIn(SignInViewModel model, string returnUrl)
        {
            // Validates the received user credentials based on the view model
            if (!ModelState.IsValid)
            {
                // Displays the sign-in form if the user credentials are invalid
                return(View());
            }

            // Attempts to authenticate the user against the Xperience database
            SignInStatus signInResult = SignInStatus.Failure;

            try
            {
                signInResult = await KenticoSignInManager.PasswordSignInAsync(model.UserName, model.Password, model.SignInIsPersistent, false);
            }
            catch (Exception ex)
            {
                // Logs an error into the Xperience event log if the authentication fails
                eventLogService.LogException("MvcApplication", "SignIn", ex);
            }

            // If the authentication was successful, redirects to the return URL when possible or to a different default action
            if (signInResult == SignInStatus.Success)
            {
                string decodedReturnUrl = Server.UrlDecode(returnUrl);
                if (!string.IsNullOrEmpty(decodedReturnUrl) && Url.IsLocalUrl(decodedReturnUrl))
                {
                    return(Redirect(decodedReturnUrl));
                }
                return(RedirectToAction("Index", "Home"));
            }

            // If the 'Registration requires administrator's approval' setting is enabled and the user account
            // is pending activation, displays an appropriate message
            if (signInResult == SignInStatus.LockedOut)
            {
                // If the 'Registration requires administrator's approval' setting is enabled and the user account
                // is pending activation, displays an appropriate message
                User user = await KenticoUserManager.FindByNameAsync(model.UserName);

                if (user.WaitingForApproval)
                {
                    ModelState.AddModelError(String.Empty, "You account is pending administrator approval.");
                }

                return(View());
            }

            // If the authentication was not successful, displays the sign-in form with an "Authentication failed" message
            ModelState.AddModelError(String.Empty, "Authentication failed");
            return(View());
        }
Example #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            // Validates the received user data based on the view model
            if (!ModelState.IsValid)
            {
                return(View(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 Xperience database
            IdentityResult registerResult = IdentityResult.Failed();

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

            // If the registration was successful, signs in the user and redirects to a different action
            if (registerResult.Succeeded)
            {
                var signInResult = await KenticoSignInManager.PasswordSignInAsync(model.UserName, model.Password, true, false);

                if (signInResult == SignInStatus.LockedOut)
                {
                    // Checks if the 'Registration requires administrator's approval' settings is enabled
                    if (user.WaitingForApproval)
                    {
                        // Notifies the user that their account is pending administrator approval
                        var message = new IdentityMessage()
                        {
                            Destination = model.Email,
                            Subject     = "Account approval pending",
                            Body        = "You account is pending administrator approval"
                        };

                        await KenticoUserManager.EmailService.SendAsync(message);
                    }

                    return(RedirectToAction("RequireConfirmedAccount"));
                }

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

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