ActionFilterAttribute is an Action filtering class that is executed before an action is executed. ActionFilterAttribute is a class of MVC that specifically processes action filtering. Based on this principle, we create a permission restriction.
For example, how to access test action in HomeController
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using MvcTest. Models;
Namespace MvcTest. Controllers
{
Public class HomeController: Controller
{
//
// GET:/Home/
Public ActionResult Index ()
{
Return View ();
}
[AuthorizeFilter]
Public ActionResult test ()
{
Return Content ("authorized to access ");
}
}
}
The AuthorizeFilterAttribute. cs content is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Namespace MvcTest. Models
{
/// <Summary>
/// Permission Interception
/// </Summary>
[AttributeUsage (AttributeTargets. Class | AttributeTargets. Method, AllowMultiple = false)]
Public class AuthorizeFilterAttribute: ActionFilterAttribute
{
FilterContextInfo fcinfo;
// OnActionExecuted is called by the ASP. net mvc framework after the operation method is executed.
// OnActionExecuting is called by the ASP. net mvc framework before the operation method is executed.
// OnResultExecuted is called by the ASP. net mvc framework after the operation result is executed.
// OnResultExecuting is called by the ASP. net mvc framework before the operation result is executed.
/// <Summary>
/// It is called by the ASP. net mvc framework before the operation method is executed.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
Fcinfo = new filterContextInfo (filterContext );
// Fcinfo. actionName; // obtain the Domain Name
// Fcinfo. controllerName; get the controllerName
Bool isstate = true;
// Islogin = false;
If (isstate) // if yes
{
// Logic code
// FilterContext. Result = new HttpUnauthorizedResult (); // jump to the login page from the URL
// FilterContext. Result = new RedirectResult ("http://www.baidu.com"); // You can also jump to another site
// FilterContext. Result = new RedirectToRouteResult (new System. Web. Routing. RouteValueDictionary (new {Controller = "product", action = "Default "}));
}
Else
{
FilterContext. Result = new ContentResult {Content = @ "sorry, you do not have the permission for the current operation! "}; // Return Content directly (" sorry, you do not have permission for the current operation! ")
}
}
/// <Summary>
/// After the operation method is executed, it is called by the ASP. net mvc framework.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnActionExecuted (ActionExecutedContext filterContext)
{
Base. OnActionExecuted (filterContext );
}
/// <Summary>
/// OnResultExecuted is called by the ASP. net mvc framework after the operation result is executed.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnResultExecuted (ResultExecutedContext filterContext)
{
Base. OnResultExecuted (filterContext );
}
/// <Summary>
/// OnResultExecuting is called by the ASP. net mvc framework before the operation result is executed.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnResultExecuting (ResultExecutingContext filterContext)
{
Base. OnResultExecuting (filterContext );
}
}
Public class filterContextInfo
{
Public filterContextInfo (ActionExecutingContext filterContext)
{
# Region
// Obtain the Domain Name
DomainName = filterContext. HttpContext. Request. Url. Authority;
// Obtain the module name
// Module = filterContext. HttpContext. Request. Url. Segments [1]. Replace ('/', ''). Trim ();
// Obtain the controllerName
ControllerName = filterContext. RouteData. Values ["controller"]. ToString ();
// Obtain the ACTION name
ActionName = filterContext. RouteData. Values ["action"]. ToString ();
# Endregion
}
/// <Summary>
/// Obtain the Domain Name
/// </Summary>
Public string domainName {get; set ;}
/// <Summary>
/// Obtain the module name
/// </Summary>
Public string module {get; set ;}
/// <Summary>
/// Obtain the controllerName
/// </Summary>
Public string controllerName {get; set ;}
/// <Summary>
/// Obtain the ACTION name
/// </Summary>
Public string actionName {get; set ;}
}
}
Download DEMO