一聚教程网:一个值得你收藏的教程网站

最新下载

asp.net最强sql防注入代码

时间:2009-08-13 00:00:00 编辑:简简单单 来源:转载


SqlConnection conn = new SqlCheck().oconn();
      // 第2种调用的方法  SqlCheck.JK1986_CheckSql();
        string osql = "select count(*) from admin";
        SqlCommand ocmd = new SqlCommand(osql, conn);

asp.net最强sql防注入代码asp.net最强sql防注入代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient ;
///


/// SqlCheck 的摘要说明
///

// #region ,#endregion 用于VS代码显示的更美观,能够折叠展开                     
public class SqlCheck
{
 public SqlCheck()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //  
    }

   
    public SqlConnection oconn()
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["StudyConnectionString"].ToString();
        //第1种调用的方法   JK1986_CheckSql();
        JK1986_CheckSql();
        if ( conn.State == ConnectionState.Closed  )
        {
            conn.Open();
        }
        return conn;
    }


    public DataTable  getsource(string getds)
    {
        SqlConnection conn = oconn();
        SqlDataAdapter da = new SqlDataAdapter(getds, conn);
        DataSet ds = new DataSet();
        da.Fill(ds,"news" );
        return ds.Tables["news"];
    }
   

    public static  void JK1986_CheckSql()
    {
        string jk1986_sql = "exec↓select↓drop↓alter↓exists↓union↓and↓or↓xor↓order↓mid↓asc↓execute↓xp_cmdshell↓insert↓update↓delete↓join↓declare↓char↓sp_oacreate↓wscript.shell↓xp_regwrite↓'↓;↓--";
        string[] jk_sql = jk1986_sql.Split('↓');
        foreach (string jk in jk_sql)
        {
            // -----------------------防 Post 注入-----------------------
            if ( System.Web.HttpContext.Current.Request.Form != null)
            {
                for (int k = 0; k < System.Web.HttpContext.Current.Request.Form.Count; k++)
                {
                    string getsqlkey = System.Web.HttpContext.Current.Request.Form.Keys[k];
                    string getip;
                    if (System.Web.HttpContext.Current.Request.Form[getsqlkey].ToLower().Contains(jk) == true)
                    {
                       System.Web.HttpContext.Current.Response.Write("<script Language=JavaScript>alert('ASP.NET( C#版本 )防注入程序提示您,请勿提交非法字符!↓\n\nBlog:http://hi.baidu.com/ahhacker86 \n\nBy:

文章评论

热门栏目