• 技术文章 >后端开发 >C#.Net教程

    c#如何防止sql注入?

    青灯夜游青灯夜游2020-09-17 09:25:05原创3612
    对于网站的安全性,是每个网站开发者和运营者最关心的问题。网站一旦出现漏洞,那势必将造成很大的损失。为了提高网站的安全性,首先网站要防注入。

    下面我们给大家介绍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(',');
      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");
      }
      }

    方法三:

    使用字符串过滤类

     /**//// < summary>
      /// 处理用户提交的请求
      /// < /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('请勿非法提交!');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('请勿非法提交!');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 = "'|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#教程

    以上就是c#如何防止sql注入?的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:c# 防止sql注入
    上一篇:sqlhelper类是什么 下一篇:c语言的代码是什么?
    PHP编程就业班

    相关文章推荐

    • 怎么防止sql注入?• web安全之如何防止SQL注入• java如何防止sql注入• mysql如何防止sql注入

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网

    "】。","pubDate": "2019-05-10T17:24:48","upDate": "2020-09-17T09:25:05","data": {"WebPage": {"headline": "c#如何防止sql注入","wapUrl": "//m.m.sbmmt.com/article/418624.html","pcUrl": "//m.sbmmt.com/m/csharp-article-418624.html","fromSrc": "php中文网","domain": "电子科技","category": ["问答"],"isDeleted": 0},"Question": [{"acceptedAnswer":"防SQL注入,最简单的办法是杜绝SQL拼接,在【C#】中可以通过对字符串过滤来防SQL注入,代码为【System.Web.HttpContext.Current.Response.Write(""】。"}],"ImageObject": [{"contentUrl": "//m.sbmmt.com/img/upload/article/000/000/024/5cd541109cd3f947.jpg","scale": "5:2"}],"Author": [{"name": "php中文网","jobTitle": ["php公益学习平台"],"headPortrait": "https://img.php.cn/upload/article/000/000/003/5d1b23156bf94358.png"}]}}