国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

c#.net全站防止SQL注入類的代碼

來源:懂視網 責編:小采 時間:2020-11-27 22:41:32
文檔

c#.net全站防止SQL注入類的代碼

c#.net全站防止SQL注入類的代碼: 代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary>/// 防SQL注入檢查器/// </summary>public class SqlChecker{ //當前請求對象 p
推薦度:
導讀c#.net全站防止SQL注入類的代碼: 代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary>/// 防SQL注入檢查器/// </summary>public class SqlChecker{ //當前請求對象 p

代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// 防SQL注入檢查器
/// </summary>
public class SqlChecker
{
    //當前請求對象
    private HttpRequest request;
    //當前響應對象
    private HttpResponse response;
    //安全Url,當出現Sql注入時,將導向到的安全頁面,如果沒賦值,則停留在當前頁面
    private string safeUrl = String.Empty;

    //Sql注入時,可能出現的sql關鍵字,可根據自己的實際情況進行初始化,每個關鍵字由'|'分隔開來
    //private const string StrKeyWord = @"select|insert|delete|from|count(|drop table|update|truncate|asc(|mid(|char(|xp_cmdshell|exec master|netlocalgroup administrators|:|net user|""|or|and";
    private const string StrKeyWord = @"select|insert|delete|from|drop table|update|truncate|exec master|netlocalgroup administrators|:|net user|or|and";
    //Sql注入時,可能出現的特殊符號,,可根據自己的實際情況進行初始化,每個符號由'|'分隔開來
    //private const string StrRegex = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'";
    private const string StrRegex = @"=|!|'";
    public SqlChecker()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }
    /// <summary>
    /// 由此構造函數創建的對象,在驗證Sql注入之后將停留在原來頁面上
    /// </summary>
    /// <param name="_request">當前請求的 Request 對象</param>
    /// <param name="_response">當前請求的 Response 對象</param>
    public SqlChecker(HttpRequest _request, HttpResponse _response)
    {
        this.request = _request;
        this.response = _response;
    }
    /// <summary>
    /// 由此構造函數創建的對象,在驗證Sql注入之后將請求將導向由 _safeUrl 指定的安全url頁面上
    /// </summary>
    /// <param name="_request">當前請求的 Request 對象</param>
    /// <param name="_response">當前請求的 Response 對象</param>
    /// <param name="_safeUrl">驗證Sql注入之后將導向的安全 url</param>
    public SqlChecker(HttpRequest _request, HttpResponse _response, string _safeUrl)
    {
        this.request = _request;
        this.response = _response;
        this.safeUrl = _safeUrl;
    }
    /// <summary>
    /// 只讀屬性 SQL關鍵字
    /// </summary>
    public string KeyWord
    {
        get
        {
            return StrKeyWord;
        }
    }
    /// <summary>
    /// 只讀屬性過濾特殊字符
    /// </summary>
    public string RegexString
    {
        get
        {
            return StrRegex;
        }
    }
    /// <summary>
    /// 當出現Sql注入時需要提示的錯誤信息(主要是運行一些客戶端的腳本)
    /// </summary>
    public string Msg
    {
        get
        {
            string msg = "<script type='text/javascript'> "
            + " alert('請勿輸入非法字符!'); ";

            if (this.safeUrl == String.Empty)
                msg += " window.location.href = '" + request.RawUrl + "'";
            else
                msg += " window.location.href = '" + safeUrl + "'";

            msg += "</script>";
            return msg;
        }
    }
    /// <summary>
    /// 檢查URL參數中是否帶有SQL注入的可能關鍵字。
    /// </summary>
    /// <returns>存在SQL注入關鍵字時返回 true,否則返回 false</returns>
    public bool CheckRequestQuery()
    {
        bool result = false;
        if (request.QueryString.Count != 0)
        {
            //若URL中參數存在,則逐個檢驗參數。
            foreach (string queryName in this.request.QueryString)
            {
                //過慮一些特殊的請求狀態值,主要是一些有關頁面視圖狀態的參數
                if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                    continue;
                //開始檢查請求參數值是否合法
                if (CheckKeyWord(request.QueryString[queryName]))
                {
                    //只要存在一個可能出現Sql注入的參數,則直接退出
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
    /// <summary>
    /// 檢查提交表單中是否存在SQL注入的可能關鍵字
    /// </summary>
    /// <returns>存在SQL注入關鍵字時返回 true,否則返回 false</returns>
    public bool CheckRequestForm()
    {
        bool result = false;
        if (request.Form.Count > 0)
        {
            //若獲取提交的表單項個數不為0,則逐個比較參數
            foreach (string queryName in this.request.Form)
            {
                //過慮一些特殊的請求狀態值,主要是一些有關頁面視圖狀態的參數
                if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                    continue;
                //開始檢查提交的表單參數值是否合法
                if (CheckKeyWord(request.Form[queryName]))
                {
                    //只要存在一個可能出現Sql注入的參數,則直接退出
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
    /// <summary>
    /// 檢查_sword是否包涵SQL關鍵字
    /// </summary>
    /// <param name="_sWord">需要檢查的字符串</param>
    /// <returns>存在SQL注入關鍵字時返回 true,否則返回 false</returns>
    public bool CheckKeyWord(string _sWord)
    {
        bool result = false;
        //模式1 : 對應Sql注入的可能關鍵字
        string[] patten1 = StrKeyWord.Split('|');
        //模式2 : 對應Sql注入的可能特殊符號
        string[] patten2 = StrRegex.Split('|');
        //開始檢查 模式1:Sql注入的可能關鍵字 的注入情況
        foreach (string sqlKey in patten1)
        {
            if (_sWord.IndexOf(" " + sqlKey) >= 0 || _sWord.IndexOf(sqlKey + " ") >= 0)
            {
                //只要存在一個可能出現Sql注入的參數,則直接退出
                result = true;
                break;
            }
        }
        //開始檢查 模式1:Sql注入的可能特殊符號 的注入情況
        foreach (string sqlKey in patten2)
        {
            if (_sWord.IndexOf(sqlKey) >= 0)
            {
                //只要存在一個可能出現Sql注入的參數,則直接退出
                result = true;
                break;
            }
        }
        return result;
    }
    /// <summary>
    /// 執行Sql注入驗證
    /// </summary>
    public void Check()
    {
        if (CheckRequestQuery() || CheckRequestForm())
        {
            response.Write(Msg);
            response.End();
        }
    }
}

使用說明 :
代碼如下:

// 使用時可以根據需要決定是要進行全局性(即針對整個應用程序)的Sql注入檢查
// ,還是局部性(即在針對某個頁面)的Sql注入檢查


/*=========== 全局性設置:在Global.asax.cs 中加上以下代碼 =============

protected void Application_BeginRequest(Object sender, EventArgs e)
{
SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response);
//或 SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response,safeUrl);
SqlChecker.Check();
}
 

/*============ 局部性:在任何時候都可直接用以下代碼來實現Sql注入檢驗 ===============

SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response);
//或 SqlChecker SqlChecker = new SqlChecker(this.Request,this.Response,safeUrl);
SqlChecker.Check();

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

c#.net全站防止SQL注入類的代碼

c#.net全站防止SQL注入類的代碼: 代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary>/// 防SQL注入檢查器/// </summary>public class SqlChecker{ //當前請求對象 p
推薦度:
標簽: 類型 net c#
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产精品久久成人影院 | 在线观看欧美精品 | 免费一级毛片 | 国产精彩视频 | 四虎国产精品免费久久久 | 国产成人a毛片 | 免费一区二区视频 | 国产欧美综合在线观看第七页 | 一97日本道伊人久久综合影院 | 人人爽天天碰天天躁夜夜躁 | 九九51精品国产免费看 | 国产日韩欧美在线 | 国产精品免费观看视频 | 精品国产一区二区二三区在线观看 | 极品美女户外勾搭无套 | 日韩欧美伊人久久大香线蕉 | 久久精品成人一区二区三区 | 国产在线成人一区二区 | 黄色毛片在线观看 | 在线国产一区二区三区 | 美国一级大黄大色毛片视频一 | 色翁荡息又大又硬又粗又爽 | xx中文字幕乱偷avxx | 久久一区二区精品综合 | 在线观看亚洲一区 | 日韩精品成人 | 久久久久9999| 亚洲国产精久久久久久久 | 久久精品视频一区二区三区 | 免费黄色在线观看 | 亚洲va国产日韩欧美精品 | 欧美日韩1区 | 欧美一级网站 | 欧美日韩另类在线 | 中文在线字幕 | 国产中文在线观看 | 成人欧美一区二区三区黑人 | 国产精选免费视频 | 国产黄色片在线观看 | 国产精品久久久久影院色 | 亚洲第一网站在线观看 |