日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C#防SQL注入代碼的實(shí)現(xiàn)方法

系統(tǒng) 5119 0

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

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

  一:丟棄SQL語(yǔ)句直接拼接,雖然這個(gè)寫(xiě)起來(lái)很快很方便。

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

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

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

  C#防SQL注入方法一

  在Web.config文件中, < appSettings>下面增加一個(gè)標(biāo)簽:如下

  < appSettings>

  < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />

  < /appSettings>

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

  C#防SQL注入方法二 www.yztrans.com

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

  protected void Application_BeginRequest(Object sender, EventArgs e){

  String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString()。Split(',');

  for(int i= 0 ;i < safeParameters.Length; i++){

  String parameterName = safeParameters[i].Split('-')[0];

  String parameterType = safeParameters[i].Split('-')[1];

  isValidParameter(parameterName, parameterType);

  }

  }

  public void isValidParameter(string parameterName, string parameterType){

  string parameterValue = Request.QueryString[parameterName];

  if(parameterValue == null) return;

  if(parameterType.Equals("int32")){

  if(!parameterCheck.isInt(parameterValue)) Response.Redirect("parameterError.aspx");

  }

  else if (parameterType.Equals("USzip")){

  if(!parameterCheck.isUSZip(parameterValue)) Response.Redirect("parameterError.aspx");

  }

  else if (parameterType.Equals("email")){

  if(!parameterCheck.isEmail(parameterValue)) Response.Redirect("parameterError.aspx");

  }

  }

  C#防SQL注入方法三

  使用字符串過(guò)濾類(lèi)

  using System;

  namespace web.comm

  {

  /**//// < summary>

  /// ProcessRequest 的摘要說(shuō)明。

  /// < /summary>

  public class ProcessRequest

  {

  public ProcessRequest()

  {

  //

  // TODO: 在此處添加構(gòu)造函數(shù)邏輯

  //

  }

  SQL注入式攻擊代碼分析#region SQL注入式攻擊代碼分析

  /**//// < summary>

  /// 處理用戶(hù)提交的請(qǐng)求

  /// < /summary>

  public static void StartProcessRequest()

  {

  // System.Web.HttpContext.Current.Response.Write("< script>alert('dddd');< /script>");

  try

  {

  string getkeys = "";

  //string sqlErrorPage = System.Configuration.ConfigurationSettings.AppSettings["CustomErrorPage"].ToString();

  if (System.Web.HttpContext.Current.Request.QueryString != null)

  {

  for(int i=0;i< System.Web.HttpContext.Current.Request.QueryString.Count;i++)

  {

  getkeys = System.Web.HttpContext.Current.Request.QueryString.Keys[i];

  if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.QueryString[getkeys],0))

  {

  //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");

  System.Web.HttpContext.Current.Response.Write("< script>alert('請(qǐng)勿非法提交!');history.back();< /script>");

  System.Web.HttpContext.Current.Response.End();

  }

  }

  }

  if (System.Web.HttpContext.Current.Request.Form != null)

  {

  for(int i=0;i< System.Web.HttpContext.Current.Request.Form.Count;i++)

  {

  getkeys = System.Web.HttpContext.Current.Request.Form.Keys[i];

  if (!ProcessSqlStr(System.Web.HttpContext.Current.Request.Form[getkeys],1))

  {

  //System.Web.HttpContext.Current.Response.Redirect (sqlErrorPage+"?errmsg=sqlserver&sqlprocess=true");

  System.Web.HttpContext.Current.Response.Write("< script>alert('請(qǐng)勿非法提交!');history.back();< /script>");

  System.Web.HttpContext.Current.Response.End();

  }

  }

  }

  }

  catch

  {

  // 錯(cuò)誤處理: 處理用戶(hù)提交信息!

  }

  }

  /**//// < summary>

  /// 分析用戶(hù)請(qǐng)求是否正常

  /// < /summary>

  /// < param name="Str">傳入用戶(hù)提交數(shù)據(jù)< /param>

  /// < returns>返回是否含有SQL注入式攻擊代碼< /returns>

  private static bool ProcessSqlStr(string Str,int type)

  {

  string SqlStr;

  if(type == 1)

  SqlStr = "exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";

  else

  SqlStr = "'|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";

  bool ReturnValue = true;

  try

  {

  if (Str != "")

  {

  string[] anySqlStr = SqlStr.Split('|');

  foreach (string ss in anySqlStr)

  {

  if (Str.IndexOf(ss)>=0)

  {

  ReturnValue = false;

  }

  }

  }

  }

  catch

  {

  ReturnValue = false;

  }

  return ReturnValue;

  }

  #endregion

  }

  }

C#防SQL注入代碼的實(shí)現(xiàn)方法


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 永胜县| 廊坊市| 公主岭市| 阿巴嘎旗| 达尔| 沙河市| 石林| 石门县| 尉犁县| 喀喇| 长沙市| 射阳县| 七台河市| 奉新县| 永济市| 左云县| 尉氏县| 济宁市| 眉山市| 贵定县| 胶南市| 洛宁县| 佳木斯市| 垫江县| 罗山县| 应用必备| 太湖县| 玉门市| 墨竹工卡县| 桑日县| 朔州市| 梁河县| 舒兰市| 湛江市| 罗源县| 陵川县| 吉首市| 蒙阴县| 若尔盖县| 盐边县| 手游|