Ejemplo n.º 1
0
        public async Task <IHttpActionResult> PasswordReset(PasswordResetModel pwd)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            IdentityUser idUser = null;

            using (Db.Operations ops = new Db.Operations())
            {
                idUser = await ops.FindUserByEmail(pwd.Email);

                if (idUser != null)
                {
                    IdentityResult result = await ops.ResetPasswordAsync(idUser.Id, pwd.Token, pwd.Password);

                    if (!result.Succeeded)
                    {
                        if (result.Errors != null && result.Errors.Count() > 0)
                        {
                            return(Redirect(pwd.FailUrl + "?error=" + GetErrorString(result)));
                        }
                    }
                    else
                    {
                        return(Redirect(pwd.SuccessUrl));
                    }
                }
            }

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> PasswordResetRequest(string email)
        {
            IdentityUser idUser = null;

            using (Db.Operations ops = new Db.Operations())
            {
                idUser = await ops.FindUserByEmail(email);

                if (idUser != null)
                {
                    try
                    {
                        // For the Password Reset Token to work:
                        // Go to the Application Pool for the web site in IIS
                        // Advanced Settings > Process Model > Load User Profile = True
                        string resetToken = await ops.GetPasswordResetToken(idUser.Id);

                        if (Settings.EmailClient != null && Settings.PasswordReset != null)
                        {
                            EmailMessage mailMessage = Settings.PasswordReset;
                            mailMessage.To = new EmailAddress(email);
                            mailMessage.PlainTextContent = mailMessage.PlainTextContent.Replace("{EmailAddress}", email).Replace("{Token}", HttpUtility.UrlEncode(resetToken));
                            mailMessage.HtmlContent      = mailMessage.HtmlContent.Replace("{EmailAddress}", email).Replace("{Token}", HttpUtility.UrlEncode(resetToken));
                            await Settings.EmailClient.SendAsync(mailMessage);
                        }

                        return(Ok());
                    }
                    catch (Exception ex)
                    {
                        return(BadRequest(string.Format("Password reset details could not be sent to {0}. Internal error: {1}", email, ex.Message)));
                    }
                }
            }

            return(Ok());
        }