示例#1
0
        public bool SendResetPasswordEmail(string userName, string htmlEmbedLink)
        {
            try
            {
                if (string.IsNullOrEmpty(userName))
                    return false;
                MailMessage mail = new MailMessage();

                SmtpClient smtpServer = new SmtpClient();
                mail.From = new MailAddress(supportEmail);
                mail.To.Add(userName);
                mail.Bcc.Add(supportEmail);
                mail.Subject = "Please reset your password within 48 hours upon receiving this email";
                mail.Body = string.Format(@"To reset your password, <a href='{0}'> please click the link and enter new password.</a>",
                    htmlEmbedLink);
                mail.IsBodyHtml = true;

                var emailToSave = new EmailModel();
                emailToSave.EmailTo = TextUtils.TokenDelimitedText(mail.To.ToList().Select(p => p.Address), ";");
                emailToSave.EmailFrom = mail.From.Address;
                emailToSave.Subject = mail.Subject;
                emailToSave.ContentHtml = emailToSave.ContentText = mail.Body;
                emailToSave.MailTypeId = lookupMgr.GetAllEmailTypes().FirstOrDefault(p => p.EmailTypeCd == "PWDRC").EmailTypeId;
                var emailMgr = new EmailManager();
                emailMgr.SaveEmail(emailToSave);

                smtpServer.Send(mail);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }    
        public ActionResult SendEmail(FormCollection collection)
        {
            try
            {
                var emailMgr = new EmailManager();
                MailMessage mail = new MailMessage();

                SmtpClient smtpServer = new SmtpClient();
                mail.From = new MailAddress(emailMgr.SupportEmail);
                mail.To.Add(emailMgr.SupportEmail);
                mail.Subject = "Request from Portal user";
                mail.Body = "How to recover password?";

                smtpServer.Send(mail);

                return RedirectToAction("Contact", "Home");
            }
            catch(Exception e)
            {
                return View("Contact");
            }
        }
 public ActionResult RetrievePassword(SecurityQuestionViewModel model)
 {
     model.IsAnswerStep = false;
     lookupMgr.GetAllSecurityQuestions().ToList()
         .ForEach(p => model.QuestionList.Add(new SelectListItem() { Text = p.SecurityQuestionDescription, Value = p.SecurityQuestionID.ToString() }));
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     else
     {
         if (model.FirstAnswer.Equals(model.UserAnswer1)
              && model.SecondAnswer.Equals(model.UserAnswer2)
              && model.ThirdAnswer.Equals(model.UserAnswer3))
         {
             // reset password token expires in 2 days
             string resetPasswordToken = webSecurity.GeneratePasswordResetToken(model.UserName, 2880);
             string resetLink = this.Url.RouteUrl("ResetPwdRoute", new { controller = "Account", action = "ResetPassword", id = resetPasswordToken, usr = model.UserName }, this.Request.Url.Scheme);
             var emailMgr = new EmailManager();
             emailMgr.SendResetPasswordEmail(model.UserName, resetLink);
             //return RedirectToAction("SingleMessage", new { message = "An Email is sent to your account to reset your password." });
             return RedirectToAction("SingleMessageWithButton", new
             {
                 message = "An Email is sent to your account to reset your password.",
                 strController = "Account",
                 strAction = "Login",
                 strButton = "Go to login"
             });
         }
         else
         {
             ViewBag.Message = "One or more answers are incorrect."; 
             return View(model);
         }
     }
 }
        public ActionResult Register(UserViewModel model)
        {
            if (webSecurity.UserExists(model.UserName))
            {
                ModelState.AddModelError("UserExistsErr", "User with same user name already exists.");
                TempData["UserExists"] = true;
            }
            if (ModelState.IsValid)
            { 
                // Attempt to register the user
                try
                {
                    // randomly generate strong password, 9 characters long
                    model.Password = RandomPassword.Generate(9); 
                    string userToken = accountMgr.CreateUser(model);
                    int userId = webSecurity.GetUserId(model.UserName);
                    accountMgr.InitForceChangePassword(userId);
                    // save encrypted password history
                    accountMgr.SavePasswordHist(model.UserName, model.Password);
                    var emailMgr = new EmailManager();
                    string confirmLink = this.Url.Action("AgreementConfirm", "Account", new { id = userToken }, this.Request.Url.Scheme);
                    //emailMgr.SendRegisterUserEmail(model, confirmLink);
                    return RedirectToAction("ListUsers"); 
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        ExceptionManager.WriteToEventLog(string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                            eve.Entry.Entity.GetType().Name, eve.Entry.State), "Application", System.Diagnostics.EventLogEntryType.Error);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            ExceptionManager.WriteToEventLog(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                ve.PropertyName, ve.ErrorMessage), "Application", System.Diagnostics.EventLogEntryType.Error);
                        }
                    }
                    throw;
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException(string.Format("Error from creating user: {0}", e.InnerException.ToString()), e.InnerException);
                }
            }

            // If we got this far, something failed, redisplay form
            model = new UserViewModel();
            ModelState.Clear();

            //Roles.GetAllRoles().ToList().ForEach(p => model.AllRoles.Add(new SelectListItem() { Text = p, Value = p }));

            // get all time zone
            model.AllStates.Add(new SelectListItem() { Text = "Select State", Value = "Select" });
            accountMgr.GetAllStates().ToList().ForEach(p => model.AllStates.Add(new SelectListItem() { Text = p.Value, Value = p.Key }));
            return View(model);
        }