2011-06-29 249 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