First, the use of actionfilterattribute to achieve rights management
1. Create a menu filter class
//Building a Permissions menu filter Public classMvcmenufilter:actionfilterattribute {Private BOOL_isenable =true;//whether to turn on filtering PublicMvcmenufilter () {_isenable =true; }//Constructor 1, open by default PublicMvcmenufilter (BOOLisenable) {_isenable = isenable;}//Constructor 2, specifying whether to open the//rewrite onactionexecuting Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {if(_isenable)//If filtering is turned on { //gets the currently executing address varRoute =filterContext.RouteData.Values; varURL =string. Format ("/{0}/{1}/{2}", route[" Area"], route["Controller"], route["Action"]); //access the database to check if the current user has this permission if(!NewSys_userservice (). Authorizeusermenu (URL)) Filtercontext.result=NewContentresult () {Content ="you do not have permission to access this feature, please contact the administrator! " }; } //Perform access Base. OnActionExecuting (Filtercontext); } }
2. Add this filter to the filter configuration file, then all actions will pass through this filter
Public classFilterconfig { Public Static voidregisterglobalfilters (globalfiltercollection filters) {filters. ADD (NewMvchandleerrorattribute ()); //filters. ADD (New System.Web.Mvc.AuthorizeAttribute ()); //filters. ADD (New Mvcdisposefilter ());Filters. ADD (NewMvcmenufilter ());//Join the Permissions menu filter } }
3, the configuration file Web. config in the <system.web> to join the following node, validation failed to jump to that address
Content reference: Https://msdn.microsoft.com/zh-cn/library/532aee0e.aspx
Question: Does this and the above prompt conflict
<mode= "Forms"> <loginurl = "~/login" timeout= "2880"/> </Authentication >
"Building a Mvc+log4net+knockout+easyui+fluentdata+sqlite framework from scratch" X, implementation of login module