• customer service QQ:    WeChat: Contact number: 18613993467     

ASP.NET MVC3 How to implement custom permission system judgment and implementation

Suyou Network (10 years of experience), serves tens of thousands of companies, and has fixed transparent quotations.Domain name registration, hosting /One-stop service for server and website source code.A physical company, a professional team, worth choosing!Website building business: corporate website construction, company official website production, foreign trade website construction, website imitation, website copying, imitation site, foreign trade website construction, single-page customer service WeChat【 suyoufuwu】


    ASP.NET Have it membership Role permission system, but this built-in system is not flexible and powerful enough, and sometimes it is complicated to operate. Here, Suyou Network introduces how to automatically determine permissions based on controllers and methods, which is simple and convenient.

    First, define a class inheritance ActionFilterAttribute class, then re-method OnActionExecuting  Write the following code

    public override void OnActionExecuting(ActionExecutingContext filterContext)

        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //No login to perform operations
            }
            else
            {
   
                //Determine whether a character exists
                FormsIdentity user = (FormsIdentity)HttpContext.Current.User.Identity;
 
                var au = db.AdminUser.Where(a => a.username == user.Name).ToList();
                if (au.Count > 0)
                {
                    // string purview= au[0].group.purview;
                    bool is_authorize = true;
                    string error_msg = "No permission to access!";
                    string model = filterContext.RouteData.Values["controller"].ToString();
                    string action = filterContext.RouteData.Values["action"].ToString();
 
                    BLLAdminUser admin_user = new BLLAdminUser();
                    string purview=admin_user.getCheckPurview(model, action);//Judgment permissions
                    string mypurview = admin_user.getMyPurview();
                    is_authorize=admin_user.inPurview(purview, mypurview);
                    if (!is_authorize)
                    {
                         //If verification does not pass the execution method
                    }
 
                }
 
            }
          
        }


    Then before the controller or method [Authorize]Just verify the mark.

The above is shared by Suyou.com with everyone." ASP.NET MVC3 How to determine and implement custom permission system ", thank you very much for your patience to finish this article, we will provide you with more information on reference, use or learning and communication.We can also provide you with:Enterprise website construction, website imitation, website copying, imitation site, foreign trade website construction, foreign trade website construction, company official website productionFor services, our company serves customers with the service concept of "integrity, professionalism, pragmatism and innovation".If you need cooperation, please scan the code to consult, and we will serve you sincerely.

TAG Label:

National branch station

National branch station