Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
[HttpPost]
[ActionName("PasswordRecover")]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public ActionResult PasswordRecover_Post(LostPassword model)
{
    if (ModelState.IsValid)
    {
        MembershipUser User;
        RegisterLogic register = new RegisterLogic();
        UpdateModel(model);
        if (register.IsExist(model) == true)
        {
            var foundUserName = register.getName(model);
            if (foundUserName != null)
            {

                User = Membership.GetUser(foundUserName);
            }
            else
            {
                User = null;
            }


            if (User != null)
            {
                var token = WebSecurity.GeneratePasswordResetToken(User.UserName);
                string resetLink = "<a href='" + Url.Action("ResetPassword", "Pages", new { rt = token }, "http") + "'>Reset Password Link</a>";

                // Email stuff
                string subject = "Reset your password for 1 KEY HPC";
                string body = "Password recovery link: " + resetLink;
                string from = "donotreply@gmail.com";
                //message.Subject = subject;
                //message.Body = body;
                MailMessage message = new MailMessage(from, model.Email,subject,body);
                SmtpClient client = new SmtpClient();

                // Attempt to send the email
                try
                {
                    client.Send(message);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Issue sending email: " + e.Message);
                }
            }
            else // Email not found
            {
                ModelState.AddModelError("", "No user found by that email.");
            }
        }
        else
        {
            ModelState.AddModelError("", "Email address does not exist.");
        }
    }
    return View(model);

}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900