1. 程式人生 > >MVC中利用ActionFilterAttribute過濾關鍵字

MVC中利用ActionFilterAttribute過濾關鍵字

在開發過程中,有時候會對使用者輸入進行過濾,以便保證平臺的安全性。遮蔽的方法有很多種,但是今天我說的這種主要是利用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("<", "&lt").Replace(">", "&gt");
  28:              if (!string.IsNullOrEmpty(t.TSite))
  29:                  t.TSite = t.TSite.Replace("<", "&lt").Replace(">", "&gt");
  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:  }

這樣,我們就完成了對輸入資料的過濾操作,下面看看結果吧:

QQ截圖20140422211039

我們可以清楚的看到,輸入結果,輸出後,一對尖角號被轉義了。