【问题标题】:I cant enter characters in password field. I can enter number only我可以在密码字段中输入字符。我只能输入数字
【发布时间】: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


【解决方案1】:

第一个选项:在您的INSERT SQL 中指定列。

INSERT INTO Users (UserID, Col2, Col3 ...) VALUES (...)

第二个选项:查看query 的实际含义并使用 SQL Server Management Studio 对其进行调试。

在这一行设置断点并检查 query 的 Locals。

mycommand = new SqlCommand(query, myconnection);

第三个选项:使用参数化查询并在命令的参数属性中设置值。

那些在你之后维护它的人会感谢你,你会在这部分代码中摆脱 SQL 注入的可能性。

【讨论】:

  • 你能详细解释一下吗?我是 asp.net 的初学者,感谢您的帮助。
  • 第二个选项和第三个选项
  • 第二个,你对调试应用程序一点都不熟悉吗?
  • 对于第二个选项,我不知道如何使用 SQL Server Management Studio 对其进行调试。我用 Visual Studio 调试
【解决方案2】:

您的代码存在很多问题。

它会泄漏连接,SQL 注入的时机已经成熟,而且非常脆弱。一次拿这些。

首先,您应该将每个实现 IDisposable 的对象包装在 using 子句中。这是确保在适当的时间清理对象的最佳实践方式。看这个问题的例子:c# closing sqlconnection and sqldatareader or not?

在很少使用的系统上,您可能不会注意到连接池回收的问题。但是,一旦您获得任何级别的流量,此代码就会以有趣且有时无法预测的方式爆炸。

其次,为了帮助防止 sql 注入,请勿使用字符串连接来构建查询。而是使用参数。 @decyclone 的答案显示了如何执行此操作的示例。破解你现在拥有的密码将是微不足道的。

第三,您的查询非常脆弱。您的插入语句取决于用户表中字段的顺序,因此该表永远不会被修改。第一次重新排序这些字段或添加新字段时,您的代码将中断。直到运行时你才会发现这个错误。您应该明确设置要插入的字段。例如:

insert into Users(Username,Password) values('SomeUser', 'SomePassword')

【讨论】:

  • 我不明白你所有的答案。我不知道什么是 sql 指令。我遵循@decyclone 的回答,正如我所说,我有另一个自动生成的字段 UserId 。当我运行代码并输入我的数据时,出现以下错误(将 nvarchar 值“UserId”转换为数据类型 int 时转换失败)。
  • @shihab:有关 SQL 注入的信息,请参阅此内容:en.wikipedia.org/wiki/SQL_injection
  • 我现在明白我已经更改了我的代码并遵循@decyclone 的回答
【解决方案3】:

您似乎正试图将 varchar 值插入数据库中的 int 类型列。

更好的方法是使用Command Parameters 来防止此类错误。

示例:

SqlCommand command = new SqlCommand("Insert into Users(UserName, Password) Values(@UserName, @Password)", connection);
command.Parameters.AddWithValue("@UserName", "SomeUser"); // You pass something else instead of "SomeUser"
command.Parameters.AddWithValue("@Password", "Password"); // You pass something else instead of "Password"
command.ExecuteNonQuery();

【讨论】:

  • 我尝试了您的代码,并且在运行它时还添加了用户 ID 字段,但出现错误(将 nvarchar 值 'UserId' 转换为数据类型 int 时转换失败)。 id 是自动生成的
  • @Shihab:如果用户标识是自动生成的,那么它不应该出现在您的插入语句中。将其取出并按原样运行 decyclone 的答案。
  • 我按照你的回答,我得到了这个错误(无法将值 NULL 插入列 'UserId',表 'test.dbo.Users';列不允许空值。插入失败。语句有已终止。)
  • @shihab:听起来用户 ID 不是身份列。你能编写表格并提供它的定义吗?
  • 它有 10 个字段(UserId、UserName、Password 和其他字段。UserId 数据类型为 int 而不是主键。
猜你喜欢
  • 1970-01-01
  • 2014-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-11
  • 2019-11-26
  • 1970-01-01
相关资源
最近更新 更多