Filter for permission control

Source: Internet
Author: User

Use the idea of AOP to make our systems configurable. Enhances system maintainability and code reusability, makes business logic clearer, and separates public parts (logs, permissions, transactions, workflows, and so on).

The idea of AOP, see blog: http://blog.csdn.net/u010785685/article/details/44139437

My AOP design concept in software development is more and more widely used, this is not a tall thing, but every programmer should be familiar with a thing. Because it is convenient for us programmers. With AOP, we can focus on the writing of logical code, unifying those system functions to the AOP framework to manage them and automatically coupling them at runtime.

I use filter to implement a permission control on the request URL.

When we visit the URL page, such as a can browse all pages. B can only browse a portion of the page, if there is no unified permission control, as long as the URL address is correct, everyone can access. So there is no authority to control the words. So before we go to the page, we're going to automatically perform the permission judgments I wrote.

Know exactly what I'm going to do, so how to do it?

I have customized a filter--authattribute.

1. If I want to execute the action below this controller. This controller and action are all written by ourselves, and this is just an example.

Namespace Itoo. basicplaceclient.controllers{    //Controller class, inherited Controllers public    class Mycontroller:controller    {        Public ActionResult Index ()        {            return View ();}}    }

2, before the execution, the permission to judge, the implementation of the custom I wrote the filter. Here we will take out your brief access to the Controller and action, in the cache to remove the access you have. Determine if you have permission to access the action in the controller. Without permission, give a friendly prompt directly, you do not have permission. Hey, it's kind of friendly. But if you have permission, he will continue to execute the action you want to access and present the page you want to see.

/************************************************* Olivia Zhao Group: AOP Group Description: AOP permissions control, when we access the URL address, first determine whether there is access. Date Created: March 19, 2015 11:56:15 version number: v1.0**********************************************/using system;using System.collections.generic;using system.linq;using system.web;using system.web.mvc;using ITOO. Library.core.memcache;using system.collections;namespace Itoo. basicplaceclient.controllers.attribute{//<summary>//ActionFilterAttribute is the action filter class,    This is done before an action is executed. And ActionFilterAttribute is a class of MVC that specializes in handling action filtering. A permission restriction based on this principle. </summary> public class Authattribute:actionfilterattribute {//<summary>// Called by the ASP.//</summary>//<param name= "Filtercontext" ></param> Pub            Lic override void OnActionExecuting (ActionExecutingContext filtercontext) {////get controllername name var controllername = filtercontext.routedata.values["Controller"].           ToString (); Get the domain name of the action you are about to execute var actionname = httpcontext.current.request.requestcontext.routedata.values["Action"].            ToString (); GUID selfGuid1 = Guid.NewGuid ();//application for a simulated GUID guid SelfGuid2 = Guid.NewGuid ();//application for a simulated GUID MEMCAC  Hehelper.add (Selfguid1.tostring (), "querybed", DateTime.Now.AddMinutes (20));   Controller Cache Memcachehelper.add (selfguid2.tostring (), "Index", DateTime.Now.AddMinutes (20));            Action Cache//Create a List collection list<string> guids1 = new list<string> (); The key value taken out of the cache is stored in the list guids1.            ADD (Selfguid1.tostring ()); Guids1.            ADD (Selfguid2.tostring ());            Create a data Dictionary Getkey object idictionary<string, object> getkey = new dictionary<string, object> ();            Gets a set of cache Getkey = Memcachehelper.get (GUIDS1);           Verify permissions, first verify controller foreach (keyvaluepair<string, object> kvp in Getkey) {     If there are permissions to the controller that will be accessed if (kvp. value.tostring () = = controllername)//If there is a acting permission to access foreach (Keyvaluepair<str ING, object> kvp1 in Getkey) if (KVP1.                            value.tostring () = = ActionName) {//all passes, there is an action under the controller that will access the                        Return             }}//Does not have permissions, validation does not pass Contentresult Content = new Contentresult (); Content.content = "<script type= ' text/javascript ' >alert (' permission validation does not pass!            '); History.go ( -1);</script> ";        Execution results for permissions not passed Filtercontext.result = Content; }    }}

This is the code of Authority judgment. Before using it, we need to register with Registerglobalfilters in global. Otherwise, this code will not be executed until the method executes.

3. Registration:

  Filters. ADD (New Authattribute ());

In this way, a simple permission control is implemented. Technology is shallow, it is written like this, there is nothing wrong with everyone to communicate with each other.

Personal understanding: For SPRING.AOP will be more tall on some, but the principle is very similar. Will not be tall on the, hehe, continue to study. A lot of exchanges and communication, welcome just to communicate with me.

Filter for permission control

Related Article

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.