Filter for permission control

Source: Internet
Author: User

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.

  1. Namespace Itoo. Basicplaceclient.controllers


  2. {


  3. Controller class, inherits the controllers



  4. public class Mycontroller:controller


  5. {



  6. Public ActionResult Index ()


  7. {


  8. return View ();


  9. }


  10. }


  11. }

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.

  1. Using System;


  2. Using System.Collections.Generic;


  3. Using System.Linq;


  4. Using System.Web;


  5. Using SYSTEM.WEB.MVC;


  6. Using Itoo. Library.Core.Memcache;


  7. Using System.Collections;



  8. Namespace Itoo. BasicPlaceClient.Controllers.Attribute


  9. {


  10. ///


  11. 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.


  12. ///


  13. public class Authattribute:actionfilterattribute


  14. {


  15. ///


  16. Called by the ASP framework before executing the action method


  17. ///


  18. ///


  19. public override void OnActionExecuting (ActionExecutingContext filtercontext)


  20. {


  21. Get Controllername Name


  22. var controllername = filtercontext.routedata.values["Controller"]. ToString ();



  23. Get the domain name of the action you're going to execute


  24. var actionname = httpcontext.current.request.requestcontext.routedata.values["Action"]. ToString ();



  25. GUID selfGuid1 = Guid.NewGuid ();//application for a simulated GUID


  26. GUID SelfGuid2 = Guid.NewGuid ();//application for a simulated GUID



  27. Memcachehelper.add (Selfguid1.tostring (), "querybed", DateTime.Now.AddMinutes (20));

    Controller cache


  28. Memcachehelper.add (Selfguid2.tostring (), "Index", DateTime.Now.AddMinutes (20));

    Action Cache



  29. Create a list collection


  30. List guids1 = new list ();



  31. Store the key value taken out of the cache in the list


  32. Guids1. ADD (Selfguid1.tostring ());


  33. Guids1. ADD (Selfguid2.tostring ());



  34. Create a data Dictionary Getkey object


  35. IDictionary getkey = new Dictionary ();



  36. Get a set of caches


  37. Getkey = Memcachehelper.get (GUIDS1);



  38. Verify permissions, first verify controller


  39. foreach (KeyValuePair kvp in Getkey)


  40. {


  41. If you have permissions to the controller that will be accessed


  42. if (kvp. value.tostring () = = Controllername)


  43. If you have permissions to the acting you want to access


  44. foreach (KeyValuePair kvp1 in Getkey)


  45. if (KVP1. value.tostring () = = ActionName)


  46. {


  47. All pass, there is an action under the controller that will access the


  48. Return


  49. }


  50. }


  51. No permissions, validation does not pass


  52. Contentresult Content = new Contentresult ();


  53. Content.content = "

  54. Execution result is permission not passed


  55. Filtercontext.result = Content;


  56. }



  57. }


  58. }

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:

    1. 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

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.