I am writing a detailed comment another day. The code is first posted and the comments are clear. You don't need to say more.
1 controller code: 2 Public actionresult forgotpassword () 3 {4 return view (); 5} 6 [actionname ("forgotpassword"), httppost] 7 public actionresult forgotpasswordpost (forgotpasswordmodel) 8 {9 If (modelstate. isvalid) 10 {11 var user = _ userdal. getuserbyname (model. name); 12 if (user = NULL) 13 {14 modelstate. addmodelerror ("", "the user name does not exist! "); 15 return view (); 16} 17 if (string. isnullorwhitespace (user. email) 18 {19 modelstate. addmodelerror ("", "this account is not bound to a mailbox and cannot retrieve the password! "); 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 ("", "the email has been sent. Check it carefully! "); 31 return view (); 32} 33} 34 return view (model); 35} 36 37 Dal Layer Code: 38 # region password retrieval 39 // <summary> 40 // the user's password retrieval function 41 /// </Summary> 42 /// <Param name = "userid"> user ID </param> 43 // <Param name = "username"> User Name </param> 44 // <Param name = "ip"> IP </param> 45 // <Param name = "email"> User's email address </param> 46 // <returns> </returns> 47 Public String userfindpassword (int userid, string username, String email, string IP) 48 {49 datetime expirationtime = datetime. now. adddays (1); // apply for Link expiration time 50 random = new random (); 51 var secretkey = random. next (100000,999 999 ). tostring (). topassword (); // generate random 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); // Save the application information to the database 61 dbcontext. savechanges (); 62 stringbuilder sb = new stringbuilder (); sb. append ("dear" + username + "Hello, <br/>"); 63 sb. append ("click the following link to set a new password. <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/>"); 65 sb. append ("(if you cannot click the URL link address, copy it and paste it into the address input box of the browser, and then click Press enter .) <Br/> "); 66 sb. append (" NOTE: This link is valid for 24 hours and the expired link will become invalid. It expires once you click it. <Br/> "); 67 sb. append (" We will continue to serve you with enthusiasm! <Br/> "); 68 string messagebody = sb. tostring (); 69 string res = mailinfo. sendmail (email, "retrieve password for MI", 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. user Id = 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 Database Design: 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}
User email password retrieval implementation