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.
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, inherits the controllers
public class Mycontroller:controller
{
Public ActionResult Index ()
{
return View ();
}
}
}
Copy Code
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.
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
{
///
ActionFilterAttribute is the action filter class, which is executed 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.
///
public class Authattribute:actionfilterattribute
{
///
Called by the ASP framework before executing the action method
///
///
public override void OnActionExecuting (ActionExecutingContext filtercontext)
{
Get Controllername Name
var controllername = filtercontext.routedata.values["Controller"]. ToString ();
Get the domain name of the action you're going 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
Memcachehelper.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 guids1 = new list ();
Store the key value taken out of the cache in the list
Guids1. ADD (Selfguid1.tostring ());
Guids1. ADD (Selfguid2.tostring ());
Create a data Dictionary Getkey object
IDictionary getkey = new Dictionary ();
Get a set of caches
Getkey = Memcachehelper.get (GUIDS1);
Verify permissions, first verify controller
foreach (KeyValuePair kvp in Getkey)
{
If you have permissions to the controller that will be accessed
if (kvp. value.tostring () = = Controllername)
If you have permissions to the acting you want to access
foreach (KeyValuePair kvp1 in Getkey)
if (KVP1. value.tostring () = = ActionName)
{
All pass, there is an action under the controller that will access the
Return
}
}
No permissions, validation does not pass
Contentresult Content = new Contentresult ();
Content.content = "
Execution result is permission not passed
Filtercontext.result = Content;
}
}
}
Copy Code
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 ());
Copy Code
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.
More Java Learning web Framework Learning Http://techfoxbbs.com
Filter for permission control