在開發過程中,有時候會對用戶輸入進行過濾,以便保證平台的安全性。屏蔽的方法有很多種,但是今天我說的這種主要是利用MVC中的ActionFilterAttribute屬性來實現。由於MVC天然支持AOP,所以我們這種過濾方式正好利用了MVC的這種特性。
下面請看步驟:
首先,當用戶輸入自己的名稱的時候,帶有類似<BR>的內容的時候,由於MVC默認是需要驗證內容的,所以,會拋出一張黃頁錯誤,提示用戶:從客戶端檢測到潛在風險的Request值。這種頁面是極為不友好的,同時也是我們作為開發最不想見到的頁面,屏蔽這個錯誤很簡單,就是在響應的頁面ActionResult上面加上[ValidateInput(false)]的特性,這樣當用戶提交的時候,頁面將不會再次對輸入內容做檢測。
如果容忍這樣的行為,將會對系統的安全性造成威脅,所以最好的解決方法就是講其中類似 <>等進行轉義。
下面我們就來利用ActionFilterAttribute構造自己的轉義過濾類:
1: using System.Web.Mvc;
2: using TinyFrame.Plugin.StrongTyped.Models;
3:
4: namespace TinyFrame.Plugin.StrongTyped
5: {
6: public class FilterCharsAttribute : ActionFilterAttribute
7: {
8: protected string parameterName = "t";
9: protected TestModel model;
10:
11: public override void OnActionExecuting(ActionExecutingContext filterContext)
12: {
13: base.OnActionExecuting(filterContext);
14:
15: //No Parameters, will return directly.
16: if (!filterContext.ActionParameters.ContainsKey(parameterName))
17: return;
18:
19: var t = filterContext.ActionParameters[parameterName] as TestModel;
20:
21: //No Entity data, will return directly
22: if (t == null)
23: return;
24:
25: //Replace chars that should be filtered
26: if (!string.IsNullOrEmpty(t.TName))
27: t.TName = t.TName.Replace("<", "<").Replace(">", ">");
28: if (!string.IsNullOrEmpty(t.TSite))
29: t.TSite = t.TSite.Replace("<", "<").Replace(">", ">");
30: }
31: }
32: }
第8行,代表我們的用戶輸入的實體類參數,具體的Controller代碼如下:
1: public ActionResult Index(TestModel t)
2: {
3: ViewData["ConvertedModel"] = t;
4: return View();
5: }
第11行,通過重載OnActionExecuting方法,我們可以定義自己的Filter。
第19行,將獲取的Input結果轉換成entity。
第27,29行,將潛在的危險字符進行轉義。
這樣書寫完畢之后,我們就打造了一個可以過濾掉關鍵字的Filter了。如果想要做的通用的話,需要對輸入的filterContext.ActionParameters進行遍歷,並通過反射構建實例,再通過反射字段值,實現通用的關鍵字過濾。這里我只提供思路,具體的做法就看自己了。
然后將這個方法加入到Controller中需要檢測的頁面的頭部,即可:
1: [ValidateInput(false)]
2: [FilterChars]
3: public ActionResult Index(TestModel t)
4: {
5: ViewData["ConvertedModel"] = t;
6: return View();
7: }
這樣,我們就完成了對輸入數據的過濾操作,下面看看結果吧:
我們可以清楚的看到,輸入結果,輸出后,一對尖角號被轉義了。