如果数据库中的密码是哈希值,如何创建密码恢复?

时间:2017-04-11 02:17:16

标签: c#

protected void btnPass_Click(object sender, EventArgs e) 
{ 
 //Create Connection String And SQL Statement 
 string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
 string strSelect = "SELECT UserName,Password FROM Users WHERE Email = @Email"; 


 SqlConnection connection = new SqlConnection(strConnection); 
 SqlCommand command = new SqlCommand(); 
 command.Connection = connection; 
 command.CommandType = CommandType.Text; 
 command.CommandText = strSelect; 


SqlParameter email = new SqlParameter("@Email", SqlDbType.VarChar, 50); 
 email.Value = txtEmail.Text.Trim().ToString(); 
 command.Parameters.Add(email); 


 //Create Dataset to store results and DataAdapter to fill Dataset 
 DataSet dsPwd = new DataSet(); 
 SqlDataAdapter dAdapter = new SqlDataAdapter(command); 
connection.Open(); 
 dAdapter.Fill(dsPwd); 
 connection.Close(); 
 if(dsPwd.Tables[0].Rows.Count > 0 ) 
     { 
 MailMessage loginInfo = new MailMessage(); 
 loginInfo.To.Add(txtEmail.Text.ToString()); 
 loginInfo.From = new MailAddress("YourID@gmail.com"); 
 loginInfo.Subject = "Forgot Password Information"; 


 loginInfo.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "

Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "

"; 
 loginInfo.IsBodyHtml = true; 
 SmtpClient smtp = new SmtpClient(); 
 smtp.Host = "smtp.gmail.com"; 
 smtp.Port = 587; 
 smtp.EnableSsl = true; 
 smtp.Credentials = new System.Net.NetworkCredential("YourGmailID@gmail.com", "YourGmailPassword"); 
 smtp.Send(loginInfo); 
 lblMessage.Text = "Password is sent to you email id,you can now Login"; 
 } 
 else 
 { 
 lblMessage.Text = "Email Address Not Registered"; 
 } 


 }:

1 个答案:

答案 0 :(得分:2)

散列的一部分原因是它通常很难逆转。 让别人看到忘记的密码是个坏主意,相反你应该考虑创建一个用户可以重置"有新密码。或者,您可以将密码设置为已知值,然后向他们发送新密码,并在登录后更改密码。