用户邮件找回密码实现

改天我在写详细注释吧,代码先贴出来,注释很明白了,应该无需多言。

1
Controller代码: 2 public ActionResult ForgotPassword() 3 { 4 return View(); 5 } 6 [ActionName("ForgotPassword"),HttpPost] 7 public ActionResult ForgotPasswordPost(ForgotPasswordModel model) 8 { 9 if (ModelState.IsValid) 10 { 11 var user = _userDal.GetUserByName(model.Name); 12 if (user == null) 13 { 14 ModelState.AddModelError("","用户名不存在!"); 15 return View(); 16 } 17 if (string.IsNullOrWhiteSpace(user.Email)) 18 { 19 ModelState.AddModelError("", "该账户没有绑定邮箱,无法找回密码!"); 20 return View(); 21 } 22 string res=_userDal.UserFindPassword(user.ID, user.Name, user.Email, HttpContext.Request.UserHostAddress); 23 if (!string.IsNullOrEmpty(res)) 24 { 25 ModelState.AddModelError("", res); 26 return View(); 27 } 28 else 29 { 30 ModelState.AddModelError("","邮件已发送,请注意查收!"); 31 return View(); 32 } 33 } 34 return View(model); 35 } 36 37 DAL层代码: 38 #region 密码找回 39 /// <summary> 40 /// 用户找回密码功能 41 /// </summary> 42 /// <param name="userId">用户ID</param> 43 /// <param name="userName">用户名</param> 44 /// <param name="ip">用户申请找回密码时候的IP</param> 45 /// <param name="email">用户的邮箱</param> 46 /// <returns></returns> 47 public string UserFindPassword(int userId, string userName, string email, string ip) 48 { 49 DateTime expirationTime = DateTime.Now.AddDays(1);//申请链接过期时间 50 Random random = new Random(); 51 var secretKey = random.Next(100000, 999999).ToString().ToPassword();//生成随机的Key 52 Web_UserFindPassword model = new Web_UserFindPassword 53 { 54 UserID = userId, 55 UserName = userName.Trim(), 56 IP = ip, 57 ExpirationTime = expirationTime, 58 SecretKey = secretKey 59 }; 60 dbContext.Web_UserFindPassword.Add(model);//把申请信息存到数据库去 61 dbContext.SaveChanges(); 62 StringBuilder sb = new StringBuilder(); sb.Append("亲爱的" + userName + "您好:<br/><br/>"); 63 sb.Append("点击以下链接设置新密码。<br/><br/>"); 64 sb.Append("<a href =\"http://www.vimi8.com/Member/FindPassword?SecretKey=" + model.SecretKey + "&UserId=" + model.UserID + "&ExpirationTime=" + model.ExpirationTime + "\">http://www.vimi8.com/Member/FindPassword?SecretKey=" + model.SecretKey + "&UserId=" + model.UserID + "&ExpirationTime=" + "&ExpirationTime=" + model.ExpirationTime + " </a><br/><br/>"); 65 sb.Append("(如果无法点击该URL链接地址,请将它复制并粘帖到浏览器的地址输入框,然后单击回车即可。)<br/><br/>"); 66 sb.Append("注意:本链接有效期24小时,过期链接将会失效。在您点击一次后失效。<br/><br/>"); 67 sb.Append("我们将一如既往、热忱的为您服务!<br/><br/>"); 68 string messageBody = sb.ToString(); 69 string res = MailInfo.SendMail(email, "为米网找回密码", messageBody); 70 return res; 71 } 72 73 public Web_UserFindPassword GetFindPasswordModel(string secretKey, int userId, DateTime expirationTime) 74 { 75 return dbContext.Web_UserFindPassword.FirstOrDefault(x => x.ExpirationTime.Minute == expirationTime.Minute && x.SecretKey == secretKey && x.UserID == userId); 76 } 77 78 public void DeleteFindPasswordModel(int id) 79 { 80 var find = dbContext.Web_UserFindPassword.FirstOrDefault(x => x.ID == id); 81 if (find != null) 82 { 83 dbContext.Web_UserFindPassword.Remove(find); 84 dbContext.SaveChanges(); 85 } 86 } 87 #endregion 88 89 数据库设计: 90 using System; 91 92 93 public class Web_UserFindPassword 94 { 95 public int ID { get; set; } 96 public int? UserID { get; set; } 97 public string UserName { get; set; } 98 public DateTime ExpirationTime { get; set; } 99 public string SecretKey { get; set; } 100 public string IP { get; set; } 101 }

 

posted @ 2014-10-19 23:18  人生无赖  阅读(618)  评论(0编辑  收藏  举报