int MyID = Convert.ToInt32(Request.QueryString["ID"]);
string ID = ReplaceSQLChar(MyID.ToString().Trim(), true);//替换字符
DataTable DB = MyData.GetDBOne("Article", ID);这里是获取ID参数,ID是string类型;获取ID后,然后在下面三个TextBox中输入也同样做字符替换;string User = ReplaceSQLChar(this.TextBox1.Text, true);
string Pass = ReplaceSQLChar(this.TextBox2.Text, true);
string Tel = ReplaceSQLChar(this.TextBox3.Text, true);
ReplaceSQLChar类如下:
            if (strInput == String.Empty)
            {
                return String.Empty;
            }
            else
            {
                if (ReplaceSQLAndOr)
                {
                    strInput = strInput.Replace("and", "");
                    strInput = strInput.Replace("AND", "");
                    strInput = strInput.Replace("And", "");
                    strInput = strInput.Replace("or", "");
                    strInput = strInput.Replace("OR", "");
                    strInput = strInput.Replace("Or", "");
                }
                strInput = strInput.Replace(":", "");
                strInput = strInput.Replace("'", "");
                strInput = strInput.Replace("‘", "");
                strInput = strInput.Replace(";", "");
                strInput = strInput.Replace(",", "");
                strInput = strInput.Replace("?", "");
                strInput = strInput.Replace("<", "");
                strInput = strInput.Replace(">", "");
                strInput = strInput.Replace("(", "");
                strInput = strInput.Replace(")", "");
                strInput = strInput.Replace("@", "");
                strInput = strInput.Replace("=", "");
                strInput = strInput.Replace("+", "");
                strInput = strInput.Replace("*", "");
                strInput = strInput.Replace("&", "");
                strInput = strInput.Replace("#", "");
                strInput = strInput.Replace("%", "");
                strInput = strInput.Replace("$", "");
                //返回已过滤掉SQL字符的字符串
                return strInput;
            }
我想这样应该不会出什么问题了吧???????

解决方案 »

  1.   


    int string float 。。
      

  2.   

    int 转换就能防止了,你有何必替换呢?转换不了的肯定要出错啊!
      

  3.   

    int MyID = Convert.ToInt32(Request.QueryString["ID"]);就这一句话,如果他注入写了字符什么的肯定出错了,页面报错跳转404,或者提示就OK 了!
      

  4.   


    DataTable DB = MyData.GetDBOne("Article", ID);写的是类,ID只能是string类型。int转string后替换字符是没必要的。
      

  5.   

    用参数化    string sql = "select * from T_ShoppingCart where UserID=@UserID and      GoodsID=@GoodsID";
                SqlParameter pa1 = new SqlParameter("@UserID", SqlDbType.VarChar);
                pa1.Value = UserID;            SqlParameter pa2 = new SqlParameter("@GoodsID", SqlDbType.VarChar);
                pa2.Value = GoodsID;
      

  6.   

    参数化就差不多了。。那个 周公的asp.net夜话 上有介绍
      

  7.   

    sql 全部用参数形式。。何苦自己转换啊!
      

  8.   

    你们老板Lord某天来注册啦,他兴致勃勃的输入了大名Sorb,让你偷偷的去掉了or,于是乎,老板的注册用户名成了Sb???想想后果先
      

  9.   

    你们老板Sorb某天来注册啦,他兴致勃勃的输入了大名Sorb,让你偷偷的去掉了or,于是乎,老板的注册用户名成了Sb???想想后果先
      

  10.   

    Replace("'","''")
    参数化
    http://topic.csdn.net/u/20090729/14/26381958-0d6e-4b90-bc90-d275e9621f93.html
      

  11.   

    读入时,把 or 查找成 "o_(*&^^%_r”,读出时,把 “_(*&^^%_"替换成 ""。其中 "_(*&^^%_" 是专用替代字符串,如 "公司名_ReplaceMark_被替换的符号名"即可。
    如果数据库发现可运算符号,直接报错即可。我在别的应用中曾经试过这种方法,还不错。
      

  12.   

    不反对你过滤.不过以下几条建议需要考虑:
    1.把关键词不要过滤成空,这样容易落进别人的陷井,譬如ANANDD经过你的处理后变成了
     AND
    2.既然ASP.NET提供了参数化命令,能用时就一定要用.
    3.表中字段能用int类型的,就不要用字符串,这样在取值时,直接转为int,安全性提高很多.