C# 我无法在密码字段中输入字符。我只能输入数字
我使用以下代码使用md5加密密码并将其存储在数据库中C# 我无法在密码字段中输入字符。我只能输入数字,c#,asp.net,visual-studio,C#,Asp.net,Visual Studio,我使用以下代码使用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();
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时转换失败)。看起来您正在尝试将
varchar
值插入数据库中的int
类型列
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();
}
}
一个更好的方法是使用,而不是防止这样的错误
例子:
第一个选项:指定列表中的列 第二个选项:查看
query
实际上是什么,并使用SQL Server Management Studio进行调试
在这一行上放置一个断点,并检查query
的局部变量
mycommand = new SqlCommand(query, myconnection);
第三个选项:使用参数化查询并在命令的Parameters属性中设置值
在您之后维护此功能的人将感谢您,并且您将在这部分代码中消除SQL注入的可能性。您的代码有很多问题 它会泄漏连接,SQL注入的时机已经成熟,而且非常脆弱。一次拿一个 首先,应该将实现IDisposable的每个对象包装在using子句中。这是确保在适当的时间清理对象的最佳实践方法。有关示例,请参见此问题: 在一个很少使用的系统上,您可能不会注意到连接池回收的问题。然而,一旦您获得任何级别的流量,这段代码就会以有趣、有时不可预测的方式爆炸 第二,为了防止sql注入,不要使用字符串连接来构建查询。而是使用参数@decyclone的回答给出了一个如何做到这一点的示例。破解您现在拥有的代码将是微不足道的 第三,您的查询非常脆弱。insert语句取决于users表中字段的顺序,因此该表永远不会被修改。第一次对这些字段重新排序或添加新字段时,代码将中断。直到运行时你才会发现这个bug。您应该明确设置要插入的字段。例如:
insert into Users(Username,Password) values('SomeUser', 'SomePassword')
上帝啊。。。请尽快阅读SQL注入。我不明白你的意思。感谢您的帮助运行Google搜索“SQL注入”,以便您理解为什么执行由用户输入构建的字符串是个坏主意。您能详细解释一下吗?第二个选项和第三个选项第三个选项:第二个选项,你对调试应用程序一点都不熟悉吗?第二个选项,我不知道如何用SQL Server Management Studio调试它。我使用visual studioI进行调试我尝试了您的代码,并且在运行它时添加了userid feild我遇到错误(将nvarchar值“userid”转换为数据类型int时转换失败)。id是自动的generated@Shihab:如果用户ID是自动生成的,那么它不应该出现在insert语句中。把它拿出来,按原样运行decyclone的答案。我按照你的答案做了操作,我得到了这个错误(无法将值NULL插入列'UserId',表'test.dbo.Users',列不允许空值。insert失败。语句已经终止。)@shihab:听起来用户ID不是标识列。你能为这个表编写脚本并提供它的定义吗?它有10个feld(UserId、UserName、Password和其他feild。UserId数据类型为int,而不是主键。我不理解您的所有答案。我不知道什么是sql injuction。我按照@decyclone的答案,正如我所说,我有另一个自动生成的feild UserId。当我运行代码并输入数据时,我得到了以下错误(将nvarchar值“UserId”转换为数据类型int时,转换失败)。@shihab:有关SQL注入的信息,请参见此:我现在了解的是,我已经更改了代码并遵循@decyclone的答案
insert into Users(Username,Password) values('SomeUser', 'SomePassword')