1.正则表达式验证

  public static bool sqlValidate(string sql)
        {
            string badStr = @"\b(update|delete|insert|or|not|like|trancate|into|exec|master|drop|execute|net user|xp_cmdshell|go|create|grant|group_concat|restore|backup|net +localgroup +administrators|iframe|cookie|location|prompt|confirm|script|<a.*|<img.*)\b";
            Regex reg = new Regex(badStr, RegexOptions.IgnoreCase);
            return reg.IsMatch(sql);
        }

     3.过滤

前端过滤

function htmlEncode(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&amp;")
    .replace(/</g, "&lt;");
    .replace(/>/g, "&gt;");
   .replace(/ /g, "&nbsp;");
   .replace(/\'/g, "&#39;");
    .replace(/\"/g, "&quot;");
   .replace(/\n/g, "<br>");
    return s;
}
//解码
function htmlDecode(str) {
    var s = "";
    if (str.length == 0) return "";
    .replace(/>/g, "&")
    .replace(/</g, "<")
    .replace(/>/g, ">")
    .replace(/ /g, " ")
    .replace(/'/g, "\'")
    .replace(/"/g, "\"")
    .replace(/<br>/g, "\n");
    return s;
}

后端过滤

 public static string FilterHTML(string html)
        {
            if (html == null)
                return "";
            //将<转义成
            string strResult = html;
            strResult = strResult.Replace("<", "&lt;")
                    .Replace(">", "&gt;")
                    .Replace("&", "&amp;")
                    .Replace("\"", "&quot;");
            return strResult;
        }

更多推荐