Esempio n. 1
0
        public ActionResult ResetAccount(ResetAccountModel model)
        {
            string recaptchaprivatekey = BgResources.Recaptcha_PrivateKeyHttp;

            try
            {
                if (!ReCaptcha.Validate(privateKey: recaptchaprivatekey))
                {
                    ModelState.AddModelError("recaptcha", Resources.AppMessages.Error_Recaptcha);
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("recaptcha", Resources.AppMessages.Error_Recaptcha_Key);
            }

            if (ModelState.IsValid)
            {
                User user = AccountServices.FindUser(usr => usr.Email == model.Email);
                if (user == null)
                {
                    ModelState.AddModelError("Email", Resources.AppMessages.Error_Email_Not_Exist);
                    return(View(model));
                }
                try
                {
                    string token = null;
                    token = CodeFirstSecurity.GeneratePasswordResetToken(user.Username, tokenExpirationInMinutesFromNow: 1440);
                    SmtpClient client = new SmtpClient {
                        Host = BgResources.Email_Server, Port = Int32.Parse(BgResources.Email_SmtpPort), EnableSsl = BgResources.Email_SSL, Credentials = new NetworkCredential(BgResources.Email_UserName, BgResources.Email_Password)
                    };
                    UserMailer.PasswordReset(token, user).Send(new SmtpClientWrapper {
                        InnerSmtpClient = client
                    });
                    ViewBag.Email = model.Email;
                    return(View("CompleteResetAccount"));
                }
                catch (InvalidOperationException ex)
                {
                    ModelState.AddModelError("UserName", ex.Message);
                }
                catch (Exception ex)
                {
                    throw new SmtpException(Resources.AppMessages.Error_SendMail);
                }
            }
            return(View(model));
        }
Esempio n. 2
0
        public ActionResult ResetPassword(string username)
        {
            if (username != null && CodeFirstSecurity.UserExists(username))
            {
                MembershipUser user = Membership.GetUser(username);

                string token = CodeFirstSecurity.GeneratePasswordResetToken(username);

                SmtpClient smtp = new SmtpClient("localhost", 25);

                MailMessage message = new MailMessage();
                message.Body = "Go here to reset you password...  http://" + HttpContext.Request.Url.Authority + "/Account/ResetPasswordNew?token=" + Server.UrlEncode(token);
                message.From = new MailAddress("*****@*****.**");
                message.To.Add(new MailAddress(user.Email));
                message.Subject = "You suck!";

                smtp.Send(message);
            }

            return(RedirectToAction("ResetPasswordEmailSent"));
        }