成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

網站安全性:C#防SQL注入代碼的實現方法

開發 后端
為了提高網站的安全性,首先網站要防注入。本文就講解了C#防SQL注入代碼實現方法,供大家參考。

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

下面說下網站防注入的幾點要素。

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

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

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

四:屏蔽SQL,javascript等注入(很是主要的),對于每個文件寫是不太可能的。所以要找到對所有文件起作用的辦法。我在網上收集了以下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"等,其中"-"前面表示參數的名稱比如:OrderId,后面的int32表示數據類型。

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: 在此處添加構造函數邏輯  
  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">傳入用戶提交數據< /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.     }  

【編輯推薦】

  1. 淺析C#啟動停止SQL數據庫服務之方法
  2. 總結C#獲取當前路徑的7種方法
  3. 淺析C# treeview控件的使用方法
  4. C#多態性的概念及其應用
  5. 介紹C#構造函數的使用方法
責任編輯:book05 來源: hi.baidu
相關推薦

2021-10-19 06:05:20

網站安全網絡威脅網絡攻擊

2009-09-01 16:29:01

C#單元測試

2019-08-21 17:10:13

安全技術網絡安全網站

2019-11-12 16:39:43

黑客網絡安全云計算

2010-05-17 16:26:36

IIS安全

2012-08-29 09:29:28

SQL Server

2017-03-01 11:52:30

2011-03-11 14:05:41

2009-11-30 09:41:38

2012-12-28 09:36:08

SQL Server SQL Server安

2013-03-04 09:21:23

SQL Server安全工具

2010-12-15 10:13:22

2023-03-07 08:00:00

2012-10-16 09:28:49

SaaS安全加密

2012-10-16 10:02:08

加密SaaS安全SaaS加密

2010-09-14 19:50:55

2020-12-11 06:05:46

智能門鎖指紋人工智能

2010-09-14 19:29:00

2010-11-09 15:50:47

SQL Server安

2009-08-05 15:04:14

C# dll注入
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 成人精品一区二区 | 欧美视频三区 | 久久国品片 | 伊人春色成人 | 成人欧美一区二区三区色青冈 | 欧美日韩一区二区三区不卡视频 | 久久美女网 | 国产成人高清成人av片在线看 | av一二三区| 成人国产精品视频 | 一区二区三区在线看 | 午夜精品一区二区三区三上悠亚 | 日本人和亚洲人zjzjhd | 中文字幕av网站 | 四虎影院在线播放 | 国产综合久久久 | 精品欧美激情在线观看 | 一区二区三区av夏目彩春 | 久久综合久久综合久久综合 | 羞羞的视频在线观看 | 亚洲一区在线播放 | 午夜av免费| 天天精品在线 | 成人深夜小视频 | 日韩精品一区二区三区中文在线 | 国产精品日韩欧美一区二区三区 | 欧美在线高清 | 亚洲v日韩v综合v精品v | 欧美国产日韩在线 | 色偷偷人人澡人人爽人人模 | 欧美久久天堂 | 91精品国产综合久久久久 | h片在线看 | 91精品国产欧美一区二区成人 | 亚洲精品乱码久久久久久9色 | 国产精品国产三级国产aⅴ无密码 | 91精品久久久久久久久99蜜臂 | 精品视频在线观看 | 天堂视频免费 | 国产黄色大片在线免费观看 | 日日人人 |