<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題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關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

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

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間: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{ //當(dāng)前請(qǐng)求對(duì)象 p
        推薦度:
        導(dǎo)讀c#.net全站防止SQL注入類的代碼: 代碼如下:using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary>/// 防SQL注入檢查器/// </summary>public class SqlChecker{ //當(dāng)前請(qǐng)求對(duì)象 p

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

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

            //Sql注入時(shí),可能出現(xiàn)的sql關(guān)鍵字,可根據(jù)自己的實(shí)際情況進(jìn)行初始化,每個(gè)關(guān)鍵字由'|'分隔開(kāi)來(lái)
            //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注入時(shí),可能出現(xiàn)的特殊符號(hào),,可根據(jù)自己的實(shí)際情況進(jìn)行初始化,每個(gè)符號(hào)由'|'分隔開(kāi)來(lái)
            //private const string StrRegex = @"-|;|,|/|(|)|[|]|}|{|%|@|*|!|'";
            private const string StrRegex = @"=|!|'";
            public SqlChecker()
            {
                //
                // TODO: 在此處添加構(gòu)造函數(shù)邏輯
                //
            }
            /// <summary>
            /// 由此構(gòu)造函數(shù)創(chuàng)建的對(duì)象,在驗(yàn)證Sql注入之后將停留在原來(lái)頁(yè)面上
            /// </summary>
            /// <param name="_request">當(dāng)前請(qǐng)求的 Request 對(duì)象</param>
            /// <param name="_response">當(dāng)前請(qǐng)求的 Response 對(duì)象</param>
            public SqlChecker(HttpRequest _request, HttpResponse _response)
            {
                this.request = _request;
                this.response = _response;
            }
            /// <summary>
            /// 由此構(gòu)造函數(shù)創(chuàng)建的對(duì)象,在驗(yàn)證Sql注入之后將請(qǐng)求將導(dǎo)向由 _safeUrl 指定的安全url頁(yè)面上
            /// </summary>
            /// <param name="_request">當(dāng)前請(qǐng)求的 Request 對(duì)象</param>
            /// <param name="_response">當(dāng)前請(qǐng)求的 Response 對(duì)象</param>
            /// <param name="_safeUrl">驗(yàn)證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>
            /// 只讀屬性過(guò)濾特殊字符
            /// </summary>
            public string RegexString
            {
                get
                {
                    return StrRegex;
                }
            }
            /// <summary>
            /// 當(dāng)出現(xiàn)Sql注入時(shí)需要提示的錯(cuò)誤信息(主要是運(yùn)行一些客戶端的腳本)
            /// </summary>
            public string Msg
            {
                get
                {
                    string msg = "<script type='text/javascript'> "
                    + " alert('請(qǐng)勿輸入非法字符!'); ";

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

        使用說(shuō)明 :
        代碼如下:

        // 使用時(shí)可以根據(jù)需要決定是要進(jìn)行全局性(即針對(duì)整個(gè)應(yīng)用程序)的Sql注入檢查
        // ,還是局部性(即在針對(duì)某個(gè)頁(yè)面)的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();
        }
         

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

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

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。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{ //當(dāng)前請(qǐng)求對(duì)象 p
        推薦度:
        標(biāo)簽: 類型 net c#
        • 熱門焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 三年片在线观看免费大全电影 | 亚欧乱色国产精品免费视频| 最新欧洲大片免费在线| 日韩精品亚洲人成在线观看| 中文字幕免费不卡二区 | 亚洲精品日韩专区silk| 99爱在线观看免费完整版 | 亚洲av无码专区国产不乱码 | 免费看国产成年无码AV片| 亚洲成a人片毛片在线| 又粗又大又黑又长的免费视频| 亚洲国产精品专区| A级毛片内射免费视频| 亚洲第一第二第三第四第五第六| 麻豆国产精品入口免费观看| 国产精品亚洲二区在线| 老司机亚洲精品影视www| a在线免费观看视频| 亚洲欧洲精品一区二区三区| 成人a视频片在线观看免费| 国产成人综合亚洲一区| 亚洲午夜无码久久久久| 久久国产乱子伦精品免费看| 亚洲六月丁香六月婷婷色伊人 | 国产精品亚洲精品爽爽| 亚洲日韩精品无码专区网址| 在线看片免费人成视久网| 精品国产日韩久久亚洲| 亚洲成a人在线看天堂无码| 精品亚洲永久免费精品| 亚洲人成7777| 国产亚洲精品国看不卡| 91免费在线播放| 特黄特色的大片观看免费视频| 亚洲Av综合色区无码专区桃色| 亚洲免费人成视频观看| h片在线播放免费高清| 久久精品国产亚洲AV久| 亚洲精品无码你懂的网站| 免费福利视频导航| 亚洲精品偷拍视频免费观看|