MVC过滤整个项目输入关键字
第1步驟.新建過濾關(guān)鍵字實現(xiàn)類:Proce***equest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SaaS.Admin.Base;
namespace SaaS.Admin.Controllers
{
? ? public class Proce***equest
? ? {
? ? ? ? public void ProcessCheckkeyWord()
? ? ? ? {
? ? ? ? ? ? #region 過濾URL提交過來的參數(shù)【檢查是否存在關(guān)鍵字】
? ? ? ? ? ? string[] keywors = { "javascript", "vbscript", "jscript", "script", "eval", "<", ">", "\'", "\"", "&", "#" };
? ? ? ? ? ? string returnUrl="/";//url地址
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? string httpmethodType = System.Web.HttpContext.Current.Request.HttpMethod;
? ? ? ? ? ? ? ? if (httpmethodType != "")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? string haskeyword = "";
? ? ? ? ? ? ? ? ? ? if (httpmethodType.ToLower() == "get")
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? System.Collections.Specialized.NameValueCollection geturlparm = System.Web.HttpContext.Current.Request.QueryString;
? ? ? ? ? ? ? ? ? ? ? ? if (System.Web.HttpContext.Current.Request.UrlReferrer != null) //?
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? returnUrl = System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery.ToString();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? int counts = geturlparm.AllKeys.Count();
? ? ? ? ? ? ? ? ? ? ? ? string[] urlparms = geturlparm.AllKeys;
? ? ? ? ? ? ? ? ? ? ? ? if (counts > 0 && urlparms.Length > 0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < urlparms.Length; i++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string parmvalue = System.Web.HttpContext.Current.Request.QueryString[i];//對應(yīng)參數(shù)值
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int j = 0; j < keywors.Length; j++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (parmvalue != "" || !string.IsNullOrEmpty(parmvalue))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (parmvalue.IndexOf(keywors[j].ToLower()) != -1)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? haskeyword = haskeyword + keywors[j] + ",";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if (haskeyword != "")
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? string newkeyws = "";
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (haskeyword.EndsWith(","))
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? haskeyword = haskeyword.Substring(0, haskeyword.Length - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #region 過濾掉重復(fù)出現(xiàn)的關(guān)鍵字符串
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string[] keyws = haskeyword.Split(',');
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (keyws.Length > 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? keyws = keyws.ToList().Distinct().ToArray();//過濾掉重復(fù)的字符
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < keyws.Length; i++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (i == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? newkeyws = keyws[i];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? newkeyws = newkeyws + "," + keyws[i];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? newkeyws = newkeyws.Replace("\'", "'").
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Replace("\"", """);//把\'單引號,\"雙引號修改為中文的單雙引號
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #endregion
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? string script = "<script>window.alert('參數(shù)存在不安全字符');" + " </" + "script>";
? ? ? ? ? ? ? ? ? ? ? ? ? ? string strScript = "<script charset='utf-8' Language=Javascript>if( confirm('參數(shù)存在不安全字符:" + newkeyws + "') ) {window.history.back(-1); } else { window.history.back(-1);}</script>";
? ? ? ? ? ? ? ? ? ? ? ? ? ? StringBuilder sb = new StringBuilder();
? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("<html>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("<head>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("<meta charset=\"utf-8\" />");
? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append(strScript);
? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("</head>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("</html>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.Write(sb.ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.End();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else if (httpmethodType.ToLower() == "post")
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? System.Collections.Specialized.NameValueCollection urlparm = System.Web.HttpContext.Current.Request.Form;
? ? ? ? ? ? ? ? ? ? ? ? if (System.Web.HttpContext.Current.Request.UrlReferrer != null) //?
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? returnUrl = System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery.ToString();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? int counts = urlparm.AllKeys.Count();
? ? ? ? ? ? ? ? ? ? ? ? string[] urlparms = urlparm.AllKeys;
? ? ? ? ? ? ? ? ? ? ? ? if (counts > 0 && urlparms.Length > 0)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < urlparms.Length; i++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string parmvalue = System.Web.HttpContext.Current.Request.Form[i];//對應(yīng)參數(shù)值
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int j = 0; j < keywors.Length; j++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (parmvalue != "" || !string.IsNullOrEmpty(parmvalue))
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (parmvalue.IndexOf(keywors[j].ToLower()) != -1)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? haskeyword = haskeyword + keywors[j] + ",";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? if (haskeyword != "")
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (haskeyword.EndsWith(","))
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? haskeyword = haskeyword.Substring(0, haskeyword.Length - 1);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #region 過濾掉重復(fù)出現(xiàn)的關(guān)鍵字符串
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string[] keyws = haskeyword.Split(',');
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string newkeyws = "";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (keyws.Length>0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? keyws = keyws.ToList().Distinct().ToArray();//過濾掉重復(fù)的字符
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < keyws.Length; i++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (i == 0)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? newkeyws = keyws[i];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? newkeyws =newkeyws+ "," + keyws[i];
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? newkeyws = newkeyws.Replace("\'", "'").
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Replace("\"", """);//把\'單引號,\"雙引號修改為中文的單雙引號
? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #endregion
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? string strScript = "<script charset='utf-8' Language=Javascript>if( confirm('參數(shù)存在不安全字符:" + newkeyws + "') ) {window.history.back(-1); } else { window.history.back(-1);}</script>";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? StringBuilder sb=new StringBuilder();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("<html>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("<head>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("<meta charset=\"utf-8\" />");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append(strScript);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("</head>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sb.Append("</html>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.Write(sb.ToString());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.End();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //System.Web.HttpContext.Current.Response.Write("<script charset='utf-8' Language=Javascript>if( confirm('" + GetBytesKeyWord + "" + newkeyws + "') ) {document.location.href='" + returnUrl + "'; } else { document.location.href='" + returnUrl + "' }</script>");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //System.Web.HttpContext.Current.Response.End();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else if (httpmethodType.ToLower() == "head")
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('參數(shù)存在不安全字符');window.location = '" + returnUrl + "';</script>");
? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.End();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.Write("<script language=javascript>alert('參數(shù)存在不安全字符');window.location = '" + returnUrl + "';</script>");
? ? ? ? ? ? ? ? ? ? ? ? System.Web.HttpContext.Current.Response.End();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? }
? ? ??
? ? ? ? ? ? #endregion
? ? ? ? }
? ? }
}
第2步驟.在頁面 ?Global.asax??》Global.asax.cs 》 調(diào)用Proce***equest:ProcessCheckkeyWord()
?protected void Application_BeginRequest(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? #region //整個項目過濾關(guān)鍵字
? ? ? ? ? ? Proce***equest process = new Proce***equest();
? ? ? ? ? ? process.ProcessCheckkeyWord();
? ? ? ? ? ? #endregion
? ? ? ? }
這樣所有的頁面輸入框有關(guān)鍵字輸入都進行過濾提示!!
轉(zhuǎn)載于:https://blog.51cto.com/haihuiwei/1956312
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的MVC过滤整个项目输入关键字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017第43周三
- 下一篇: Eclipse/myeclipse中*.