濮阳杆衣贸易有限公司

主頁 > 知識庫 > c#.net全站防止SQL注入類的代碼

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

熱門標(biāo)簽:山東防封電銷卡辦理套餐 廈門四川外呼系統(tǒng) 杭州智能電話機器人 內(nèi)蒙古智能電銷機器人哪家強 怎樣在地圖標(biāo)注消火栓圖形 地圖標(biāo)注位置多的錢 泰州手機外呼系統(tǒng)軟件 濟源人工智能電話機器人價格 百度地圖標(biāo)注點擊事件

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

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

    //Sql注入時,可能出現(xiàn)的sql關(guān)鍵字,可根據(jù)自己的實際情況進行初始化,每個關(guān)鍵字由'|'分隔開來
    //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注入時,可能出現(xiàn)的特殊符號,,可根據(jù)自己的實際情況進行初始化,每個符號由'|'分隔開來
    //private const string StrRegex = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'";
    private const string StrRegex = @"=|!|'";
    public SqlChecker()
    {
        //
        // TODO: 在此處添加構(gòu)造函數(shù)邏輯
        //
    }
    /// summary>
    /// 由此構(gòu)造函數(shù)創(chuàng)建的對象,在驗證Sql注入之后將停留在原來頁面上
    /// /summary>
    /// param name="_request">當(dāng)前請求的 Request 對象/param>
    /// param name="_response">當(dāng)前請求的 Response 對象/param>
    public SqlChecker(HttpRequest _request, HttpResponse _response)
    {
        this.request = _request;
        this.response = _response;
    }
    /// summary>
    /// 由此構(gòu)造函數(shù)創(chuàng)建的對象,在驗證Sql注入之后將請求將導(dǎo)向由 _safeUrl 指定的安全url頁面上
    /// /summary>
    /// param name="_request">當(dāng)前請求的 Request 對象/param>
    /// param name="_response">當(dāng)前請求的 Response 對象/param>
    /// param name="_safeUrl">驗證Sql注入之后將導(dǎo)向的安全 url/param>
    public SqlChecker(HttpRequest _request, HttpResponse _response, string _safeUrl)
    {
        this.request = _request;
        this.response = _response;
        this.safeUrl = _safeUrl;
    }
    /// summary>
    /// 只讀屬性 SQL關(guān)鍵字
    /// /summary>
    public string KeyWord
    {
        get
        {
            return StrKeyWord;
        }
    }
    /// summary>
    /// 只讀屬性過濾特殊字符
    /// /summary>
    public string RegexString
    {
        get
        {
            return StrRegex;
        }
    }
    /// summary>
    /// 當(dāng)出現(xiàn)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參數(shù)中是否帶有SQL注入的可能關(guān)鍵字。
    /// /summary>
    /// returns>存在SQL注入關(guān)鍵字時返回 true,否則返回 false/returns>
    public bool CheckRequestQuery()
    {
        bool result = false;
        if (request.QueryString.Count != 0)
        {
            //若URL中參數(shù)存在,則逐個檢驗參數(shù)。
            foreach (string queryName in this.request.QueryString)
            {
                //過慮一些特殊的請求狀態(tài)值,主要是一些有關(guān)頁面視圖狀態(tài)的參數(shù)
                if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                    continue;
                //開始檢查請求參數(shù)值是否合法
                if (CheckKeyWord(request.QueryString[queryName]))
                {
                    //只要存在一個可能出現(xiàn)Sql注入的參數(shù),則直接退出
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
    /// summary>
    /// 檢查提交表單中是否存在SQL注入的可能關(guān)鍵字
    /// /summary>
    /// returns>存在SQL注入關(guān)鍵字時返回 true,否則返回 false/returns>
    public bool CheckRequestForm()
    {
        bool result = false;
        if (request.Form.Count > 0)
        {
            //若獲取提交的表單項個數(shù)不為0,則逐個比較參數(shù)
            foreach (string queryName in this.request.Form)
            {
                //過慮一些特殊的請求狀態(tài)值,主要是一些有關(guān)頁面視圖狀態(tài)的參數(shù)
                if (queryName == "__VIEWSTATE" || queryName == "__EVENTVALIDATION")
                    continue;
                //開始檢查提交的表單參數(shù)值是否合法
                if (CheckKeyWord(request.Form[queryName]))
                {
                    //只要存在一個可能出現(xiàn)Sql注入的參數(shù),則直接退出
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
    /// summary>
    /// 檢查_sword是否包涵SQL關(guān)鍵字
    /// /summary>
    /// param name="_sWord">需要檢查的字符串/param>
    /// returns>存在SQL注入關(guān)鍵字時返回 true,否則返回 false/returns>
    public bool CheckKeyWord(string _sWord)
    {
        bool result = false;
        //模式1 : 對應(yīng)Sql注入的可能關(guān)鍵字
        string[] patten1 = StrKeyWord.Split('|');
        //模式2 : 對應(yīng)Sql注入的可能特殊符號
        string[] patten2 = StrRegex.Split('|');
        //開始檢查 模式1:Sql注入的可能關(guān)鍵字 的注入情況
        foreach (string sqlKey in patten1)
        {
            if (_sWord.IndexOf(" " + sqlKey) >= 0 || _sWord.IndexOf(sqlKey + " ") >= 0)
            {
                //只要存在一個可能出現(xiàn)Sql注入的參數(shù),則直接退出
                result = true;
                break;
            }
        }
        //開始檢查 模式1:Sql注入的可能特殊符號 的注入情況
        foreach (string sqlKey in patten2)
        {
            if (_sWord.IndexOf(sqlKey) >= 0)
            {
                //只要存在一個可能出現(xiàn)Sql注入的參數(shù),則直接退出
                result = true;
                break;
            }
        }
        return result;
    }
    /// summary>
    /// 執(zhí)行Sql注入驗證
    /// /summary>
    public void Check()
    {
        if (CheckRequestQuery() || CheckRequestForm())
        {
            response.Write(Msg);
            response.End();
        }
    }
}

使用說明 :

復(fù)制代碼 代碼如下:

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


/*=========== 全局性設(shè)置:在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();
}
 

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

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

您可能感興趣的文章:
  • php中防止SQL注入的最佳解決方法
  • PHP中防止SQL注入實現(xiàn)代碼
  • php防止SQL注入詳解及防范
  • 有效防止SQL注入的5種方法總結(jié)
  • 防止xss和sql注入:JS特殊字符過濾正則
  • JS代碼防止SQL注入的方法(超簡單)
  • PHP中防止SQL注入攻擊和XSS攻擊的兩個簡單方法
  • discuz的php防止sql注入函數(shù)
  • PHP簡單實現(xiàn)防止SQL注入的方法
  • 有效防止sql注入的方法演示

標(biāo)簽:喀什 新鄉(xiāng) 朔州 洛陽 朝陽 周口 臺州 百色

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《c#.net全站防止SQL注入類的代碼》,本文關(guān)鍵詞  c#.net,全站,防止,SQL,注入,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《c#.net全站防止SQL注入類的代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于c#.net全站防止SQL注入類的代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    搜索| 文山县| 普洱| 丹阳市| 怀集县| 安康市| 岗巴县| 达尔| 监利县| 嘉定区| 安泽县| 大厂| 四会市| 丰都县| 冀州市| 中阳县| 邵东县| 门头沟区| 且末县| 汉川市| 上饶市| 遵化市| 固始县| 修文县| 松原市| 黄梅县| 化州市| 莎车县| 岐山县| 连平县| 舟曲县| 锦州市| 邢台市| 西华县| 玉屏| 寿阳县| 青冈县| 洛隆县| 隆安县| 宣化县| 临汾市|