代碼片段:
if (reader.Read()) {
int UserID = reader.GetInt32(0);
string Password = reader["UserPassword"].ToString();
string Password0 = tbUserPassword.Text;
}
reader.Read():推進到下一行,**返回 true 表示有數據**,false 表示結束。reader.GetInt32(0):讀取第 0 列(通常為 ID),強類型獲取 int。reader["UserPassword"]:按列名讀取密碼列,**返回 object**,需 ToString()。tbUserPassword.Text:獲取前端文本框用戶輸入的密碼明文。if (Password == Password0) {
// 登錄成功,跳轉
Session["UserID"] = UserID;
Response.Redirect("Main.aspx");
} else {
lblError.Text = "密碼錯誤";
}
hash = BCrypt.HashPassword(password) 存 hash。BCrypt.Verify(password, hash) 驗證。cmd.Parameters.AddWithValue("@user", username)。