asp.net实现忘记密码找回的代码

发布时间:2019-08-02编辑:脚本学堂
分享一例asp.net实现的忘记密码找回的代码,通过发送邮件,然后邮件确认的方式找回密码。

1,找回密码的页面
 

复制代码 代码示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
 <head id="Head1" runat="server">
 <title>个人会员密码找回_http://www.jb200.com</title>
 <link href="/style.css" rel="stylesheet" type="text/css" />
 <script language="javascript" type="text/javascript">
 // 检查 E-mail 是否已被注册
 function CheckEmail()
 {
 var e = document.getElementById("email").value;
 if(e != "")
 {
 if(!/(S)+[@]{1}(S)+[.]{1}(w)+/.test(e))
 {
 alert("请输入格式正确的E-mail 地址!");
 var email = document.getElementById ( "email" );
 email.value = "";
 email.focus ();
 }
 }
 }
 
 function checkAll()
 {
 var ee = document.getElementById("username").value;
 if(ee == "")
 {
 alert('登录名称不能为空');
 return false;
 }
 
 var e = document.getElementById("email").value;
 if(e == "")
 {
 alert('Emial不能为空');
 return false;
 }
 }
 </script>
 </head>
 <body>
 <form id="form1" runat="server">
 <div>
 <table width="778" border="0" align="center" cellpadding="5" cellspacing="0" class="table_huang">
 <tr>
 <td colspan="2" class="TD_huang_14_write"> <span style="font-size: 11pt">>> 获取密码</span></td>
 </tr>
 <tr>
 <td width="26"></td>
 <td width="724"><table width="90%" border="0" cellspacing="2" cellpadding="5">
 <tr>
 <td width="23%"><div align="right">用户名:</div></td>
 <td width="77%">
 <asp:TextBox ID="username" runat="server" Width="150px" ></asp:TextBox></td>
 </tr>
 <tr>
 <td><div align="right">注册时填写的Email:</div></td>
 <td>
 <asp:TextBox ID="email" runat="server" Width="200px" ></asp:TextBox></td>
 </tr>
 <tr>
 <td><div align="right">验证码:</div></td>
 <td><table width="100%">
 <tr>
 <td align="left" valign="top" style="width: 64px"><asp:TextBox ID="txt_validcode" runat="server" Height="15px" Width="62px"></asp:TextBox> </td>
 <td align="left" valign="top"><img id="yzm" src="../ValidateCode.aspx" /></td>
 </tr>
 </table></td>
 </tr>
 <tr>
  <td colspan="2" style="text-align: center">
 <asp:Button ID="Button1" runat="server" Text=" 找回密码 " /></td>
 </tr>
 </table> </td>
 </tr>
 </table>
 </div>
 </form>
 </body>
 </html>

2,后台代码(找回密码功能)
 

复制代码 代码示例:
using System.Data; 
 using System.Configuration; 
 using System.Collections; 
 using System.Web; 
 using System.Web.Security; 
 using System.Web.UI; 
 using System.Web.UI.WebControls; 
 using System.Web.UI.WebControls.WebParts; 
 using System.Web.UI.HtmlControls; 
 public partial class ClientServer_Default2 : System.Web.UI.Page 
 { 
 string lspass = ""; 
  
 protected void Page_Load(object sender, EventArgs e) 
 { 
 if (!IsPostBack) 
 { 
 this.Button1.Attributes.Add("onclick", "javascript:return checkAll();"); 
 }
 } 
 protected void Button1_Click(object sender, EventArgs e) 
 { 
 HttpCookie myCookie_validcode = Request.Cookies["FWCX_CheckCode"]; 
 if (myCookie_validcode.Value.ToString() != txt_validcode.Text) 
 { 
 Response.Write("<script>"); 
 Response.Write("alert('验证码错误')"); 
 Response.Write("</script>"); 
 return; 
 }
 //首先判断该登录名称和email是否存在  
 string sql = "select * from t_users where u_username='" + username.Text.Trim() + "' and u_email='" + email.Text.Trim() + "' and u_class='0' and u_valid='1'";
 DataTable dt = new DataTable(); 
 //dt = new SQLTool().dataTable(sql);  
  
 if (dt.Rows.Count < 1) 
 {  // www.jb200.com
 Response.Write("<script>alert('登录名称或Email地址错误!,请重新填写后再试。');</script>"); 
  
 } 
 else 
 { 
 //首先给现在的数据库t_users附上一个临时密码  
 //Response.Write(CreateRandomCode(8));  
 lspass = CreateRandomCode(8); 
 sql = "update t_users set u_getpwd='" + lspass + "' where u_username='" + username.Text.Trim() + "' and u_email='" + email.Text.Trim() + "' and u_class='0' and u_valid='1'";
 //if (new SQLTool().ExecuteSql(sql) >= 0)  
 //{  
 sendEmail(email.Text.Trim(), username.Text.Trim()); 
 Response.Write("<script>alert('临时密码已经发送到您的注册Email中,请在下一页面中修改密码。');window.location.href='getpwdconfirm.aspx';</script>");
 //}  
 } 
 } 
 public void sendEmail(string email, string name) 
 { 
 try 
 { 
 jmail.Message jmessage = new jmail.Message(); 
 jmessage.Charset = "GB2312"; 
 jmessage.From = "xt45@163.com"; 
 // 发信地址   
 jmessage.MailServerUserName = "86085005"; 
 //smtp认证用户名(注:如为网易用户,不加要@163.com,只要前面部分即可)  
 jmessage.MailServerPassWord = "kingseer"; 
 // smtp论证用户名密码   
  
 jmessage.FromName = "108人力银行"; 
 // 发信人   
 jmessage.ReplyTo = "t51@163.com"; 
 // 回复地址   
 jmessage.C;//邮件内容为html  
 jmessage.Subject = "108人力银行临时密码"; 
  
 string strbody = ""; 
 strbody += new functions().getInfoXX("t_pagetexts", "p_name", "找回密码邮件反馈-个人", "p_value").Replace("$", name).Replace("@%", lspass);
 jmessage.HTMLBody = strbody; 
 // 邮件标题   
 jmessage.AddRecipient(email, "", ""); 
 jmessage.Send("smtp.163.com", false);//发送邮件smtp.163.com  
 jmessage.Close();//关闭对象,释放资源  
 } 
 catch (Exception err) 
 { 
 Response.Write(err); 
 } 
 } 
 /// <summary>  
 /// 功能:产生数字和字符混合的随机字符串  
 /// </summary>  
 /// <param name="codecount">字符串的个数</param>  
 /// <returns></returns>  
 private string CreateRandomCode(int codecount) 
 {  
 // 数字和字符混合字符串  
 string allchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n"; 
 //分割成数组  
 string[] allchararray = allchar.Split(','); 
 string randomcode = ""; 
  
 //随机数实例  
 System.Random rand = new System.Random(unchecked((int)DateTime.Now.Ticks)); 
 for (int i = 0; i < codecount; i++) 
 { 
 //获取一个随机数  
 int t = rand.Next(allchararray.Length); 
 //合成随机字符串  
 randomcode += allchararray[t]; 
 } 
 return randomcode; 
 }
}