c#如何防止sql注入?

青灯夜游
發布: 2020-09-17 09:25:05
原創
7173 人瀏覽過

對於網站的安全性,是每個網站開發者和經營者最關心的問題。網站一旦出現漏洞,勢必將造成很大的損失。為了提高網站的安全性,首先網站要防注入。

c#如何防止sql注入?

下面我們要介紹C#防止sql注入的幾個方法:

方法一:

在Web.config檔案下方增加一個如下標籤:

< appSettings>
  < add key="safeParameters" value="OrderID-int32,CustomerEmail-email,ShippingZipcode-USzip" />
< /appSettings>
登入後複製

其中key是< saveParameters>後面的值為」OrderId-int32」等,其中」-「前面表示參數的名稱例如:OrderId,後面的int32表示資料型別。

方法二:

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

protected void Application_BeginRequest(Object sender, EventArgs e){
  String[] safeParameters = System.Configuration.ConfigurationSettings.AppSettings["safeParameters"].ToString()。Split(&#39;,&#39;);
  for(int i= 0 ;i < safeParameters.Length; i++){
  String parameterName = safeParameters[i].Split(&#39;-&#39;)[0];
  String parameterType = safeParameters[i].Split(&#39;-&#39;)[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");
  }
  }
登入後複製

方法三:

##使用字串過濾類別

 /**//// < summary>
  /// 处理用户提交的请求
  /// < /summary>
  public static void StartProcessRequest()
  {
  // System.Web.HttpContext.Current.Response.Write("< script>alert(&#39;dddd&#39;);< /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(&#39;请勿非法提交!&#39;);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(&#39;请勿非法提交!&#39;);history.back();< /script>");
  System.Web.HttpContext.Current.Response.End();
  }
  }
  }
  }
  catch
  {
  // 错误处理: 处理用户提交信息!
  }
  }
  /**//// < summary>
  /// 分析用户请求是否正常
  /// < /summary>
  /// < param name="Str">传入用户提交数据< /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 = "&#39;|and|exec|insert|select|delete|update|count|*|chr|mid|master|truncate|char|declare";  bool ReturnValue = true;  try
  {
  if (Str != "")
  {
  string[] anySqlStr = SqlStr.Split(&#39;|&#39;);
  foreach (string ss in anySqlStr)
  {
  if (Str.IndexOf(ss)>=0)
  {
  ReturnValue = false;  }
  }
  }
  }
  catch
  {
  ReturnValue = false;  }
  return ReturnValue;  }
  #endregion  }
  }
登入後複製
相關影片教學推薦:《

C#教學

以上是c#如何防止sql注入?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!