MVC API Permissions Control

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.