本文將給出通過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="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//數組索引隨機數
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="" />