2011-06-29 181 views
4

我有一个简单的C#窗体,它充当登录,但也有一个窗体来更改用户的密码。验证和更改用户的密码

当你点击更改密码时,表单将加载一个当前密码,新通行证和确认新通行证的文本框,以及一个保存按钮。

我已经在标签中存储了用户名,以便当前的密码可以检查,如果它是从数据库中是否有效。

我在我在Microsoft SQL Server 2008中

代码是迄今为止如下创建的表中存储这些。

SqlConnection connect = new SqlConnection(str); 
connect.Open(); 
string username = label_username.Text; 
string password = textBox_Current.Text; 
string newPassword = textBox_New.Text; 
string confirmPassword = textBox_Verify.Text; 
string sqlquery = "UPDATE [Member] SET [email protected] where [email protected]"; 
SqlCommand cmd = new SqlCommand(sqlquery, connect); 
cmd.Parameters.AddWithValue("@newpass", textBox_Verify.Text); 
cmd.Parameters.AddWithValue("@username", label_username.Text); 
cmd.Parameters.AddWithValue("@password", textBox_Current.Text); 
cmd.Connection = connect; 
cmd.ExecuteNonQuery(); 
sqlDataReader reader = null; 
reader = cmd.ExecuteReader(); 
while (reader.Read()) 
{ 
if ((textBox_New.Text == reader["newPassword"].ToString()) & (textBox_Verify.Text == (reader["confirmPassword"].ToString()))) { } 
} 
MessageBox.Show("Password Changed Successfully!"); 
this.Close(); 

在执行上面的代码,密码改变,但我想:

  • 检查验证一样,如果用户已经在当前密码输入错误的密码。
  • newpassword并确认密码。
  • 当第一次保存底部空密码用户点击数据库不应该保存,而应该给消息“请输入密码”

如何才能做到这一点?

+4

不要将密码存放在纯文本。相反,请使用安全哈希。 – SLaks

+3

当然,你哈希这些密码,你只是简化它为我们的利益,对吧?对!? –

+3

我会建议只使用成员身份验证,附带API和一切,没有必要重新发明轮子 – BrokenGlass

回答

3

你真的不应该以纯文本格式存储这些密码。您应该散列密码并存储散列。然后,如果您想检查密码是否正确,请输入用户键入的密码并将其与用户存储的散列进行比较。

但是,它听起来像你需要帮助为当前用户从数据库中获取值。把这样的东西放在那里,应该为你做。请注意,就像我上面所说的那样,这实际上应该是检索密码的散列,而不是纯文本中的实际密码。

string sqlquery = "SELECT Password FROM [Member] where [email protected]"; 
SqlCommand cmd = new SqlCommand(sqlquery, connect); 
cmd.Parameters.AddWithValue("@username", label_username.Text); 
cmd.Connection = connect; 
string currentPassword = (string)cmd.ExecuteScalar(); 

if (currentPassword == textBox_Current.Text) 
{ 
// PASSWORD IS CORRECT, CHANGE IT, NOW. 
} else { 
// WOW EASY BUDDY, NOT SO FAST 
} 
0

首先,您应该在您的应用程序中使用密码散列,因此数据库的密码字段应该包含散列值。

假设这,来实现自己的目标,

  1. 考虑您的用户名字符串 - >哈希它 - >编写一个查询来检查存储在数据库中的散列值与用户密码的哈希值是否相同
  2. 考虑串密码,串NEWPASSWORD在你的代码 - >散都 - >检查散列值是否相同
  3. 考虑串密码,串NEWPASSWORD - >检查它们是否为空或长度为0

你也应该按以下顺序执行以下任务:

1 - > 3 - > 2

希望这有助于...

0
protected void btn_PasswordChange(object sender, EventArgs e) 
    { 
     string constring = DataAccess.GetConnection(); 
     SqlConnection con = new `SqlConnection`(constring); 

     { 
      if (con.State != ConnectionState.Open) 
       con.Open(); 
     } 
     string str = "select * from tbl_MemberLogin where Password='" + txtoldpwd.Text + "'"; 
     DataTable DT = new DataTable(); 
     DT = objdut.GetDataTable(str); 
     if (DT.Rows.Count == 0) 
     { 
      lblmsg.Text = "Invalid current password"; 
      lblmsg.ForeColor = System.Drawing.Color.Red; 
     } 
     else 
     { 
      SqlCommand cmd = new SqlCommand(); 
      cmd.CommandText = "update tbl_MemberLogin set Password='" + txtnewpwd.Text + "' where UserName='" + Session["UserName"].ToString() + "'"; 
      cmd.ExecuteNonQuery(); 
      lblmsg.Text = "Password changed successfully"; 
      lblmsg.ForeColor = System.Drawing.Color.Green; 
     } 
    } 
+0

请为您的代码添加一些上下文。只有代码答案不能解释你做了什么,因此作为答案无用。 – creyD