【发布时间】:2010-12-22 18:40:33
【问题描述】:
我用下面的代码用md5加密密码并存入数据库。
public partial class register : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=Shihab-PC;Initial Catalog=test;User ID=sh;Password=admin1");
SqlCommand cmd = new SqlCommand();
SqlDataAdapter ad = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myconnection;
SqlCommand mycommand;
string query;
myconnection = new SqlConnection("Data Source=Shihab-PC;Initial Catalog=test;User ID=sh;Password=admin1");
if (!Page.IsPostBack)
{
myconnection.Open();
query = "select * from Users";
mycommand = new SqlCommand(query, myconnection);
dr = mycommand.ExecuteReader();
dr.Read();
int count = Convert.ToInt16(dr[0].ToString());
while (dr.Read())
{ count++; }
TextBox4.Text = Convert.ToString(count + 1);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection myconnection;
SqlCommand mycommand;
int ra;
string query;
myconnection = new SqlConnection("Data Source=Shihab-PC;Initial Catalog=test;User ID=sh;Password=admin1");
myconnection.Open();
MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
Byte[] hashedDataBytes;
UTF8Encoding encoder = new UTF8Encoding();
hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(TextBox2.Text));
StringBuilder hex = new StringBuilder(hashedDataBytes.Length * 2);
foreach (Byte b in hashedDataBytes)
{
string x = b.ToString() + " ";
hex.AppendFormat("{0:x2}", b);
}
query = "Insert into Users values(" + TextBox4.Text + ",'" +
TextBox3.Text + "','" +
hex.ToString() + "','" +
TextBox1.Text + "','" +
TextBox5.Text + "','" +
TextBox6.Text + "','" +
TextBox7.Text + "','" +
TextBox8.Text + "','" +
TextBox9.Text + "','" +
TextBox10.Text + "')";
mycommand = new SqlCommand(query, myconnection);
ra = mycommand.ExecuteNonQuery();
if (ra > 0)
{
string msg = "alert('Record Inserted Sucessfuly')";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", msg, true);
Response.Redirect("signin.aspx");
}
else
{
string msg = "alert('Unable to Insert Record ')";
Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", msg, true);
}
myconnection.Close();
}
}
当我运行代码并在密码字段中输入字符时出现问题,我得到以下信息 错误(将 varchar 值 '1234567yY' 转换为数据类型 int 时转换失败。)
【问题讨论】:
-
好主...请尽快阅读SQL注入。
-
我没明白你的意思。谢谢你的帮助
-
在 Google 上搜索“SQL 注入”,这样您就明白为什么执行从用户输入构建的字符串是个坏主意。
标签: c# asp.net visual-studio