Example #1
0
        public async Task <ActionResult> ResetPassword(string password, string confirmPassword, string payload)
        {
            if (password != confirmPassword)
            {
                ViewData["error"] = "Detta lösenord stämmer inte överens med det första du angav.";
            }

            if (!_userManager.IsUserPasswordValid(password))
            {
                ViewData["error"] = "Lösenordet är inte av rätt typ. Se krav för nytt lösenord.";
            }

            if (ViewData["error"] != null)
            {
                ViewData["payload"] = payload;
                return(PageView("Reset password"));
            }

            payload = payload?.Replace(' ', '+');
            try
            {
                var data = _tokenGenerator.Decrypt <ResetPasswordConfirmationData>(payload);
                //check if the guid is still valid
                var checkGuidResult = await _securityRepo.CheckPasswordGuidAsync(data.UserName, data.GuidString);

                if (!checkGuidResult)
                {
                    return(new HttpStatusCodeResult(400));
                }

                //activate user
                await _userManager.ActivateAccount(data.UserName);

                //reset now
                await _securityRepo.ChangePasswordAsync(data.UserName, password);
            }
            //pretend these security error didn't happen
            catch (CryptographicException) { }
            catch (ArgumentException) { }
            catch (FaultException ex) when(ex.Message.Contains("The new password cannot be the same"))
            {
                ViewData["error"] = "Lösenordet är inte av rätt typ. Se krav för nytt lösenord.";
                return(PageView("Reset password"));
            }

            // return success anyway
            return(View("~/Views/Login/ResetPasswordSuccessful.cshtml", StartPageViewModel));
        }
        /// <summary>
        /// Create a password, associate it with user and send an activation email to that user
        /// </summary>
        private async Task SendActivationEmail(UserCore newUser)
        {
            var password = RandomPassword.Generate();
            await _securityRepo.ChangePasswordAsync(newUser.UserName, password);

            ViewData["password"] = password;
            ViewData["link"]     = ConfigurationManager.AppSettings["domainUrl"];

            var emailBody = this.RenderPartialViewToString("~/Views/Registration/Activation/ConfirmationEmailTemplate.cshtml",
                                                           ViewData);

            _logger.Info($"Sending activation email to {newUser.Email}");
            await _emailService.SendMailAsync(Email.LantmannenFromAddress, new[] { newUser.Email },
                                              new string[0], "Välkommen till LM" + "\xB2", emailBody, _ticketProvider.GetTicket())
            .ConfigureAwait(false);
        }
        public async Task <ActionResult> ChangePassword(ChangePasswordPage currentPage, string oldPassword, string newPassword, string retypeNewPassword)
        {
            var message = string.Empty;
            var incorrectPasswordMessage = string.Empty;

            var isOldPasswordCorrect = _securityRepository.ValidateUser(SiteUser.UserName, oldPassword);

            if (!isOldPasswordCorrect)
            {
                incorrectPasswordMessage = "Lösenordet du angav stämmer inte överens med ditt nuvarande lösenord.";
            }
            else if (!newPassword.Equals(retypeNewPassword))
            {
                message = "Detta lösenord stämmer inte överens med det första du angav.";
            }
            else if (!UserManager.IsUserPasswordValid(newPassword))
            {
                message = "Lösenordet är inte av rätt typ. Se krav för nytt lösenord.";
            }
            else
            {
                try
                {
                    await _securityRepository.ChangePasswordAsync(SiteUser.UserName, newPassword);

                    var settingPage = ContentExtensions.GetSettingsPage();
                    TempData["UpdatePasswordSuccess"] = true;
                    return(RedirectToAction("Index", new { node = settingPage.MyAccountLink }));
                }
                catch (FaultException)
                {
                    message = "Lösenordet är inte av rätt typ. Se krav för nytt lösenord";
                }
            }

            ViewData["message"] = message;
            ViewData["incorrectPasswordMessage"] = incorrectPasswordMessage;

            return(View("~/Views/MyProfile/ChangePassword.cshtml", new PageViewModel <ChangePasswordPage>(currentPage)));
        }