1. 程式人生 > >MVC action過濾器驗證登錄

MVC action過濾器驗證登錄

bsp new mage att cti 驗證 其他 需要 star

技術分享

方法一 : 1.創建一個全局action過濾器 (在appstart 的filterconfig中註冊 filters.Add(new LoginAttribute());)

     2.不需要登錄的contoller或者action

      則在該類或者action上添加該過濾器特性 (isNeed=false)

技術分享

方法二: 1.創建一個filter 不在全局註冊

     2. 創建 一個baseControler ,然後再basecontroller上邊添加該filter特性

3.需要登錄的則繼承該basecontroller,不需要登錄的則不繼承該basecontroller

註意: 1.OnActionExecuting 中 base.OnActionExecuting(filterContext);

如果當前項目有多個filter則加上 base.OnActionExecuting(filterContext);

不添加則不會執行其他的filter

2.filterContext.Result = new RedirectResult("/User/login");

在filter裏邊頁面跳轉用 filterContext.Result = new RedirectResult("/User/login");

如果用filterContext.HttpContext.Response.Redirect("/User/login"); 則在跳轉後還會繼續執行 後邊的action

eg: home/index 跳轉user/login 後,還會接著執行index/action 裏邊的方法

MVC action過濾器驗證登錄