public ActionResult Index(ProfileModel payload)
        {
            if (!ModelState.IsValid)
            {
                return(View(payload));
            }

            var EFUser = (User)Session["User"];

            if (EFUser == null)
            {
                ViewBag.Error = "Unable to update profile, due to user disabled or not found.";
                return(View(payload));
            }
            EFUser             = _auth.GetCredentials(EFUser.Email);
            EFUser.Name        = payload.Name;
            EFUser.Email       = payload.Email;
            EFUser.IsAnonymous = payload.IsAnonymous;

            _auth.UpdateCredentials(EFUser, payload.Password);
            Session["User"] = EFUser;

            // setup view model to fill email render the view as string to send via SMTP
            if (!string.IsNullOrEmpty(payload.Password))
            {
                var rootUrl = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

                var changePasswordEmailModel = new ChangePasswordEmailModel
                {
                    RecipientDisplayName = EFUser.Name,
                    RecipientEmail       = EFUser.Email,
                    NewPassword          = payload.Password,
                    Site = rootUrl
                };
                var emailContentHtml = StringRenderer.RenderView(changePasswordViewPath, changePasswordEmailModel, false);
                _email.Send(emailContentHtml, changePasswordEmailModel);
            }

            ViewBag.Success = "Profile updated";
            return(View("Index", payload));
        }
Пример #2
0
        public PartialViewResult Recovery(Models.EmailModel payload)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(PartialView(toastViewPath, new MessageDisplayModel
                {
                    Title = "Email not sent.",
                    Color = "danger",
                    Message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors))
                }));
            }

            var EFUser = _auth.GetCredentials(payload.Email);

            if (EFUser == null)
            {
                Response.StatusCode = (int)HttpStatusCode.NotFound;
                return(PartialView(toastViewPath, new MessageDisplayModel
                {
                    Title = "Email not sent.",
                    Color = "danger",
                    Message = "Email not registered."
                }));
            }

            try
            {
                // get new password recovery hash created to user
                var passwordRecoveryHash = _auth.ProvideLinkToRecovery(EFUser);
                // current root url to complete link
                var rootUrl = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
                // setup view model to fill email
                var passwordRecoveryModel = new PasswordRecoveryEmailModel
                {
                    RecipientEmail       = EFUser.Email,
                    RecipientDisplayName = EFUser.Name,
                    RecoveryLink         = $"{rootUrl}/Login/PasswordRecovery?email={EFUser.Email}&verifier={passwordRecoveryHash.HashCode}",
                    ExpireDate           = passwordRecoveryHash.ExpirationDate.Value,
                    Site = rootUrl
                };
                // render the view as string to send via SMTP
                var emailContentHtml = StringRenderer.RenderView(passwordRecoveryViewPath, passwordRecoveryModel, false);

                _email.Send(emailContentHtml, passwordRecoveryModel);
            }
            catch (Exception e)
            {
                Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return(PartialView(toastViewPath, new MessageDisplayModel
                {
                    Title = "Email not sent.",
                    Color = "danger",
                    Message = e.Message
                }));
            }

            Response.StatusCode = (int)HttpStatusCode.OK;
            return(PartialView(toastViewPath, new MessageDisplayModel
            {
                Title = "Email sent.",
                Color = "success",
                Message = "Por favor, verifique sua caixa de e-mail."
            }));
        }