zoukankan      html  css  js  c++  java
  • .NET防止用户恶意输入

     /// <summary>
      /// 该方法用来检测用户输入是否带有恶意
      /// </summary>
      /// <param name="text">用户输入的文字</param>
      /// <param name="maxlength">最大的长度</param>
      /// <returns>返回验证后的文字</returns>
      public static string InputText(string text, int maxlength)
      {
      text = text.ToLower().Trim();
      if (string.IsNullOrEmpty(text))
      return string.Empty;
      if (text.Length > maxlength)
      text = text.Substring(0, maxlength);

      text = Regex.Replace(text, "[\\s]{2,{", " ");
      text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
      text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //&nbsp;
      text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //any other tags
      text = Regex.Replace(text,"=", "");
      text = Regex.Replace(text, "%", "");
      text = Regex.Replace(text, "'", "");
      text = Regex.Replace(text, "select", "");
      text = Regex.Replace(text, "insert", "");
      text = Regex.Replace(text, "delete", "");
      text = Regex.Replace(text, "or", "");
      text = Regex.Replace(text, "exec", "");
      text = Regex.Replace(text, "--", "");
      text = Regex.Replace(text, "and", "");
      text = Regex.Replace(text, "where", "");
      text = Regex.Replace(text, "update", "");
      text = Regex.Replace(text, "script", "");
      text = Regex.Replace(text, "iframe", "");
      text = Regex.Replace(text, "master", "");
      text = Regex.Replace(text, "exec", "");
      text = Regex.Replace(text, "<", "");
      text = Regex.Replace(text, ">", "");
      text = Regex.Replace(text, "\r\n", "");

      return text;
      }

     

     

     

    来源于:www.hackbadboy.com  B.B.S.T 信息安全团队 BadBoy网络安全小组


  • 相关阅读:
    做事要趁早
    软件企业利润率知多少
    项目管理经验谈之意外事件处理
    App中调用iPhone的home + 电源键截屏功能
    WCF学习(一)
    10月博客学习一览
    设计模式之工厂模式读后感
    请教各位大鸟(关于附件上传)
    刚来的兄弟,一起扬帆起航吧
    巧用asp导出csv格式excel报表
  • 原文地址:https://www.cnblogs.com/secbook/p/2654921.html
Copyright © 2011-2022 走看看