国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
網(wǎng)站安全性:C#防SQL注入代碼的實現(xiàn)方法 - 51CTO.COM
    為了提高網(wǎng)站的安全性,首先網(wǎng)站要防注入。本文就講解了C#防SQL注入代碼實現(xiàn)方法,供大家參考。

    對于網(wǎng)站的安全性,是每個網(wǎng)站開發(fā)者和運營者最關(guān)心的問題。網(wǎng)站一旦出現(xiàn)漏洞,那勢必將造成很大的損失。為了提高網(wǎng)站的安全性,首先網(wǎng)站要防注入,最重要的是服務器的安全設(shè)施要做到位。

    下面說下網(wǎng)站防注入的幾點要素。

    一:丟棄SQL語句直接拼接,雖然這個寫起來很快很方便。

    二:如果用SQL語句,那就使用參數(shù)化,添加Param

    三:盡可能的使用存儲過程,安全性能高而且處理速度也快

    四:屏蔽SQL,javascript等注入(很是主要的),對于每個文件寫是不太可能的。所以要找到對所有文件起作用的辦法。我在網(wǎng)上收集了以下3種方法

    C#防SQL注入方法一

    在Web.config文件中, < appSettings>下面增加一個標簽:如下

            
    1. < appSettings>   
    2. < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />   
    3. < /appSettings>  

    其中key是 < saveParameters>后面的值為"OrderId-int32"等,其中"-"前面表示參數(shù)的名稱比如:OrderId,后面的int32表示數(shù)據(jù)類型。

    C#防SQL注入方法二

    在Global.asax中增加下面一段:  

            
    1. protected void Application_BeginRequest(Object sender, EventArgs e){   
    2. String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString().Split(',');   
    3. for(int i= 0 ;i <  safeParameters.Length; i++){   
    4. String parameterName = safeParameters[i].Split('-')[0];   
    5. String parameterType = safeParameters[i].Split('-')[1];   
    6. isValidParameter(parameterName, parameterType);   
    7. }   
    8. }   
    9.  
    10. public void isValidParameter(string parameterName, string parameterType){   
    11. string parameterValue = Request.QueryString[parameterName];   
    12. if(parameterValue == nullreturn;   
    13.  
    14. if(parameterType.Equals("int32")){   
    15. if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");   
    16. }   
    17. else if (parameterType.Equals("USzip")){   
    18. if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");   
    19. }   
    20. else if (parameterType.Equals("email")){   
    21. if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");   
    22. }   
    23. }  

    C#防SQL注入方法

    使用字符串過濾類

     

            
    1. using System;  
    2.  
    3. namespace web.comm  
    4. {  
    5.     /**//// < summary>  
    6.     /// ProcessRequest 的摘要說明。  
    7.     /// < /summary>  
    8.     public class ProcessRequest  
    9.     {  
    10.         public ProcessRequest()  
    11.         {  
    12.             //  
    13.             // TODO: 在此處添加構(gòu)造函數(shù)邏輯  
    14.             //  
    15.         }  
    16.  
    17.         SQL注入式攻擊代碼分析#region SQL注入式攻擊代碼分析  
    18.         /**//// < summary>  
    19.         /// 處理用戶提交的請求  
    20.         /// < /summary>  
    21.         public static void StartProcessRequest()  
    22.         {  
    23.               
    24. //            System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>");  
    25.             try 
    26.             {  
    27.                 string getkeys = "";  
    28.                 //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();  
    29.                 if (System.Web.HttpContext.Current.Request.QueryString != null)  
    30.                 {  
    31.       
    32.                     for(int i=0;i< System.Web.HttpContext.Current.Request.QueryString.Count;i++)  
    33.                     {  
    34.                         getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];  
    35.                         if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0))  
    36.                         {  
    37.                             //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");  
    38.                             System.Web.HttpContext.Current.Response.Write("< script>alert('請勿非法提交!');history.back();< /script>");  
    39.                             System.Web.HttpContext.Current.Response.End();  
    40.                         }  
    41.                     }  
    42.                 }  
    43.                 if (System.Web.HttpContext.Current.Request.Form != null)  
    44.                 {  
    45.                     for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++)  
    46.                     {  
    47.                         getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];  
    48.                         if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1))  
    49.                         {  
    50.                             //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");  
    51.                             System.Web.HttpContext.Current.Response.Write("< script>alert('請勿非法提交!');history.back();< /script>");  
    52.                             System.Web.HttpContext.Current.Response.End();  
    53.                         }  
    54.                     }  
    55.                 }  
    56.             }  
    57.             catch 
    58.             {  
    59.                 // 錯誤處理: 處理用戶提交信息!  
    60.             }  
    61.         }  
    62.         /**//// < summary>  
    63.         /// 分析用戶請求是否正常  
    64.         /// < /summary>  
    65.         /// < param name="Str">傳入用戶提交數(shù)據(jù)< /param>  
    66.         /// < returns>返回是否含有SQL注入式攻擊代碼< /returns>  
    67.         private static bool ProcessSqlStr(string Str,int type)  
    68.         {  
    69.             string SqlStr;  
    70.  
    71.             if(type == 1)  
    72.                 SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";  
    73.             else 
    74.                 SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";  
    75.  
    76.             bool ReturnValue = true;  
    77.             try 
    78.             {  
    79.                 if (Str != "")  
    80.                 {  
    81.                     string[] anySqlStr = SqlStr.Split('|');  
    82.                     foreach (string ss in anySqlStr)  
    83.                     {  
    84.                         if (Str.IndexOf(ss)>=0)  
    85.                         {  
    86.                             ReturnValue = false;  
    87.                         }  
    88.                     }  
    89.                 }  
    90.             }  
    91.             catch 
    92.             {  
    93.                 ReturnValue = false;  
    94.             }  
    95.             return ReturnValue;  
    96.         }  
    97.         #endregion  
    98.  
    99.     }  

    【編輯推薦】

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#防SQL注入代碼實現(xiàn)方法
C# 將數(shù)據(jù)導出到Excel匯總
突破短板,傳統(tǒng)桌面程序 使用webapi 擴展迎合web和移動端融合的需求
ASP.NET防SQL注入腳本程序 v2.0
vs2010實際項目ASP.NET小試練_完整頁綠色軟件資訊
C# 通用文件上傳類
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服