日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

MVC之ActionFilterAttribute自定义属性

發布時間:2024/9/20 c/c++ 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MVC之ActionFilterAttribute自定义属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ActionFilterAttribute里有OnActionExecuting方法,跟Controller一樣, 同是抽象實現了IActionFilter接口。

// 登錄認證特性 public class AuthenticationAttribute : ActionFilterAttribute {public override void OnActionExecuting(ActionExecutingContext filterContext){if (filterContext.HttpContext.Session["username"] == null)filterContext.Result = new RedirectToRouteResult("Login", new RouteValueDictionary { { "from", Request.Url.ToString() } });base.OnActionExecuting(filterContext);} }

使用方法如下:

public class HomeController : Controller { [Authentication] public ActionResult Index(){return View();} }

如果你想針對整個MVC項目的所有Action都使用此過濾器,步驟如下:

a. 確保Global.asax.cs的Application_Start方法中包含如下紅色行

public class MvcApplication : System.Web.HttpApplication {protected void Application_Start(){AreaRegistration.RegisterAllAreas();WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);} }

b. 在FilterConfig.cs文件中注冊相應的特性過濾器:

public class FilterConfig {public static void RegisterGlobalFilters(GlobalFilterCollection filters){filters.Add(new HandleErrorAttribute()); filters.Add(new AuthenticationAttribute());} }

如此,通過過濾器的方法實現認證和授權

?

另有不推薦的方法實現授權功能,自定義一個控制器類,再通過繼承這個控制器類:

1、繼承Controller:

1.1?參考WebForm使用方式,在派生類里自己添加了驗證方法,然后在每個Action方法里調用。

派生類如下:

public class AuthenticationControllor : Controller {public bool Validate(){if (Session["username"] == null)return false;elsereturn true;}public ActionResult RedirectLogin(bool redirect = true){if (redirect)return RedirectToAction("Login", "Home", new { from = Request.Url.ToString() });elsereturn RedirectToAction("Login", "Home");} }

?

使用類如下:

public class HomeController : AuthenticationControllor {public ActionResult Index(){if (!Validate())return RedirectLogin();return View();} }

?

1.2?改進上面的使用,通過用Controller里有一個OnActionExecuting方法,此方法是在Action之前執行的,非常方便。

派生類如下:

public class AuthenticationControllor : Controller {protected override void OnActionExecuting(ActionExecutingContext filterContext){if (filterContext.HttpContext.Session["username"] == null)filterContext.Result = new RedirectToRouteResult("Login", new RouteValueDictionary { { "from", Request.Url.ToString() } });base.OnActionExecuting(filterContext);} }

?

使用類如下:

// 不需要多寫任何邏輯代碼就能判斷是否登錄并跳轉 public class HomeController : AuthenticationControllor {public ActionResult Index(){ return View();} }

?

/// <summary>/// 權限攔截/// </summary>[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]public class PermissionFilterAttribute : ActionFilterAttribute{/// <summary>/// 權限攔截/// </summary>/// <param name="filterContext"></param>public override void OnActionExecuting(ActionExecutingContext filterContext){if (!this.CheckAnonymous(filterContext)){//未登錄驗證if (SessionHelper.Get("UserID") == null){//跳轉到登錄頁面filterContext.RequestContext.HttpContext.Response.Redirect("~/Admin/User/Login");}}}/// <summary>/// [Anonymous標記]驗證是否匿名訪問/// </summary>/// <param name="filterContext"></param>/// <returns></returns>public bool CheckAnonymous(ActionExecutingContext filterContext){//驗證是否是匿名訪問的Actionobject[] attrsAnonymous = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AnonymousAttribute), true);//是否是Anonymousvar Anonymous = attrsAnonymous.Length == 1;return Anonymous;}}

通過寫一個BaseController來進行權限的驗證,這樣就不需要所有需要驗證的Controller加標注了,當然BaseController還可以增加其他通用的處理

/// <summary>/// Admin后臺系統公共控制器(需要驗證的模塊)/// </summary> [PermissionFilter]public class BaseController:Controller{}

?

?

?其它文章 :

http://www.cnblogs.com/sunkaixuan/p/4908773.html

總結

以上是生活随笔為你收集整理的MVC之ActionFilterAttribute自定义属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。