Define a class: Testauthorizeattribute
Public classTestauthorizeattribute:authorizeattribute { Public stringRoleName =""; //Permission Entry Public Override voidonauthorization (System.Web.Http.Controllers.HttpActionContext actioncontext) {Base. Onauthorization (Actioncontext); } //Judging Permissions protected Override BOOLisauthorized (System.Web.Http.Controllers.HttpActionContext actioncontext) {if(RoleName = ="Administrator") return true; return false; } //the permission is false to execute the content protected Override voidhandleunauthorizedrequest (System.Web.Http.Controllers.HttpActionContext actioncontext) {Base. Handleunauthorizedrequest (Actioncontext); } }
The namespace referenced here is System.Web.Http, not SYSTEM.WEB.MVC. Authorizeattribute.
Define a controller: TestController
Control over the entire controller permissions
Enter this controller in case the privilege is super Administrator
[Testauthorize (Rolename=" Super admin ")]
Public class
{ publicstring getindex () { return " 123"; } }
Control of an action permission in the controller
Public class Testcontroller:apicontroller {
Enter this action " Super Administrator "if the permission is Super Administrator)] public String getindex () { return"123"; } }
MVC API Permissions Control