【发布时间】:2015-05-21 04:19:52
【问题描述】:
在创建Users 时,密码凭据以加密格式保存在数据库中。现在我想要的是,
当用户选择Forgot password 选项时,他需要填写电子邮件ID,并将相应的密码发送到他的电子邮件ID。
问题是密码仅以加密格式出现, 示例:-
3ab315c4b788dc6de20ff5f64574501f
下面是我用用户名和详细信息发送邮件的代码
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT username,password FROM tbl_User Where email= '" + txtEmail.Text.Trim() + "'", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0)
{
MailMessage Msg = new MailMessage();
// Sender e-mail address.
Msg.From = new MailAddress(txtEmail.Text);
// Recipient e-mail address.
Msg.To.Add(txtEmail.Text);
Msg.Subject = "Password Details";
Msg.Body = "Hi, <br/>Please check your Login Details<br/><br/>Your Username is: " + ds.Tables[0].Rows[0]["username"] + "<br/><br/>Your Password is: " + ds.Tables[0].Rows[0]["password"] + "<br/><br/>";
Msg.IsBodyHtml = true;
// your remote SMTP server IP.
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("test@test.com", "********");
smtp.EnableSsl = true;
smtp.Send(Msg);
Msg = null;
//lbltxt.Text = "Your Password Details Sent to your mail";
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Your Password Details Sent to your mail');window.location ='Login.aspx';", true);
// Clear the textbox valuess
txtEmail.Text = "";
}
else
{
Response.Write("<script>alert('Email Id you entered does not exist');</script>");
}
}
在以加密格式发送密码时,还可以查看我的加密代码。是MD5格式
private string md5(string sPassword)
{
MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(sPassword);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
return s.ToString();
}
请指导。
【问题讨论】:
-
MD5 是哈希函数,不是加密函数。散列函数是不可逆的,因此不可能将用户的密码散列转换回他们的密码(忽略 MD5 中的任何弱点,或一般的暴力破解或字典攻击)。任何以可恢复格式存储密码的尝试(例如,以可逆加密形式存储密码,或更糟糕的是,以纯文本形式存储)损害用户的安全。能够通过电子邮件向用户发送他们当前的密码并不是一种有效的帐户恢复机制,应该避免这种机制。
-
@Iridium:所以,请告诉我最好的替代方式。很乐意实现这一点
标签: c# asp.net email encryption