本文将给出通过C#编程来实现 忘记密码 功能的方法和代码,大家可以参照本文的思路自己写出这个功能代码……
以下是引用片段:int result = user.GetBackPassword(LoginName.Text.Trim(), Question.Text.Trim(), Answer.Text.Trim(), Email.Text); if (result == 1) { Message.Text = "您的密码已发送,请到邮箱查收"; //user.ChangePassword( } else { Message.Text = "您的输入信息有误!"; } public int GetBackPassword(string userName, string question, string answer, string email) { object m_DBNull = Convert.DBNull; //获得新的随机密码 string newPassword = MakePassword(6); //定义存储过程参数 SqlParameter[] para = { new SqlParameter("@userName", userName), new SqlParameter("@question", question), new SqlParameter("@answer", answer), new SqlParameter("@newPassword", newPassword), new SqlParameter("@result", SqlDbType.Int, 8, ParameterDirection.Output, true, 0, 0, "", DataRowVersion.Default, m_DBNull) }; //执行存储过程 try { DAL.SQLHelper.ExecuteNonQuery(DAL.SQLHelper.CONN_STRING, CommandType.StoredProcedure, "GetBackPwd", para); } catch { throw new Exception("邮件无法发送!"); } //获得输出参数的值 int result = Convert.ToInt32(para[4].Value); //如果密码保护资料填写正确 if (result == 1) { //从Web.config获取发信人地址、邮件标题、邮件用户名和密码以及SmtpServer string sender = System.Configuration.ConfigurationSettings.AppSettings["mainSender"]; string title = System.Configuration.ConfigurationSettings.AppSettings["mailTitle"]; string mailUser = System.Configuration.ConfigurationSettings.AppSettings["mailUser"]; string mailPwd = System.Configuration.ConfigurationSettings.AppSettings["mailPwd"]; string smtpServer = System.Configuration.ConfigurationSettings.AppSettings["mailSmtpServer"]; //发信 try { Mail.CDOsendmail(sender, email, title, "您在eshop的密码已找回,新密码为"+newPassword , mailUser, mailPwd, smtpServer); } catch(Exception ex) { throw new Exception(ex.Message); } } return result; } //随机生成密码 private static string MakePassword(int pwdLength) { //声明要返回的字符串 string tmpstr = ""; //密码中包含的字符数组 string pwdchars="abcdefghijklmnopqrstuvwxyz0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //数组索引随机数 int iRandNum; //随机数生成器 Random rnd = new Random(); for(int i=0;i<pwdLength;i++) { //Random类的Next方法生成一个指定范围的随机数 iRandNum = rnd.Next(pwdchars.Length); //tmpstr随机添加一个字符 tmpstr += pwdchars[iRandNum]; } return tmpstr; } ALTER PROCEDURE GetBackPwd @question nvarchar(50), @answer nvarchar(50), @userName nvarchar(50), @newPassword nvarchar(50), @result int output AS if exists (SELECT * FROM USERINFO WHERE USERNAME=@USERNAME AND QUESTION=@QUESTION AND ANSWER=@ANSWER) BEGIN SET @RESULT = 1 UPDATE USERINFO SET USERPWD = @newPassword WHERE userName = @userName END ELSE BEGIN SET @RESULT = -1 END GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO <add key="mailUser" value="" /> <add key="mailPwd" value="" /> <add key="mailSmtpServer" value="" /> <add key="mailSender" value="" /> <add key="mailTitle" value="" />