Beispiel #1
0
        public ActionResult LostPassword(LostPasswordModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipUser user;
                using (var context = new MultipartContext())
                {
                    var foundUserName = (from u in context.UserProfiles
                                         where u.Email == model.Email
                                         select u.UserName).FirstOrDefault();

                    if (foundUserName != null)
                    {
                        user = Membership.GetUser(foundUserName.ToString());
                    }
                    else
                    {
                        user = null;
                    }
                }
                if (user != null)
                {
                    // Generae password token that will be used in the email link to authenticate user
                    var token = WebSecurity.GeneratePasswordResetToken(user.UserName);
                    // Generate the html link sent via email
                    string resetLink = "<a href='" + Url.Action("ResetPassword", "Account", new { rt = token }, "http") + "'>Reset Password Link</a><br/>";

                    MailMessage mail = new MailMessage();

                    mail.To.Add(new MailAddress(model.Email));
                    mail.From = new MailAddress("*****@*****.**");
                    mail.Bcc.Add(new MailAddress("*****@*****.**"));
                    mail.Subject = "Reset your password for Multi-PART application website";
                    mail.Body    = "We have received a request to reset your password on Multi-PART application website. Please ignore this email if the request was not sent by you. <br/> If you want to reset your password, please click on the link: " + resetLink + "<br><br/> Thank you, <br/>Multi-PART Group";

                    mail.IsBodyHtml = true;

                    SmtpClient smtp = new SmtpClient();

                    // Attempt to send the email
                    try
                    {
                        smtp.Send(mail);
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", "Issue sending email: " + e.Message + "<br/>");
                    }
                }
            }
            ViewBag.sent = "yes";
            return(View(model));
        }