MVC deep analysis (I) extended built-in MVC Filter

Source: Internet
Author: User

Recently, we used the MVC built-in permission filter in our company project to implement permission control. After checking the previous code, we found that the permission filter interface has been implemented elsewhere, the function is to determine whether to log on to any page of the system. If you do not log on to the system, the system automatically reads the domain account information of the Local Machine and completes the logon. The problem is that if the newly added permission verification function is based on the permission filter, the execution sequence of the filter must be considered. Because permission verification is based on user information, therefore, I want to ensure that the user's Automatic Login filter is executed before the permission filter, but the MVC permission filter has the highest priority by default, I don't want the non-Permission authentication function to be executed in the permission filter, and want the filter to have a higher priority than the permission filter. What should I do? It seems that it is only implemented by extending the MVC built-in filter.

I. Preliminary plan
A custom IFirstFilter interface is called first in the MVC Framework. Programmers only need to create features that implement this interface to implement custom filters.

II. Implementation steps
1) override the ControllerActionInvoker method InvokeAction

Because the action method calls of the MVC Framework are uniformly executed through InvokeAction, the MVC built-in filter will be called in this method. We will overwrite it to "built-in" the custom filter. In addition, two custom methods are also defined. The GetFirstFilters method is used to obtain custom features through the reflection feature, and InvokeFirstFilters is used to execute custom filters.

Create the class file UserControllerActionInvoker. cs. The Code is as follows:

View sourceprint? 01 using System;

02 using System. Collections. Generic;

03 using System. Linq;

04 using System. Web;

05 using System. Web. Mvc;

06 using System. Reflection;

07

08 namespace MvcUserFilter. MVC

09 {

10 public class UserControllerActionInvoker: ControllerActionInvoker

11 {

12 public override bool InvokeAction (ControllerContext controllerContext, string actionName)

13 {

14 if (controllerContext! = Null &&! String. IsNullOrEmpty (actionName ))

15 {

16 ControllerDescriptor controllerDescriptor = GetControllerDescriptor (controllerContext );

17 ActionDescriptor actionDescriptor = FindAction (controllerContext, controllerDescriptor, actionName );

18

19 if (actionDescriptor! = Null)

20 {

21 IList <IFirstFilter> firstFilters = GetFirstFilters (actionDescriptor );

22 FirstFilterContext firstContext = InvokeFirstFilters (controllerContext, firstFilters, actionDescriptor );

23 if (firstContext. Result! = Null)

24 {

25 InvokeActionResult (controllerContext, firstContext. Result );

26 return true;

27}

28}

29}

30

31 return base. InvokeAction (controllerContext, actionName );

32

33}

34

35 private IList <IFirstFilter> GetFirstFilters (ActionDescriptor actionDescriptor)

36 {

37

38 MethodInfo methodInfo = (actionDescriptor as ReflectedActionDescriptor). MethodInfo;

39

40 FilterAttribute [] typeFilters = (FilterAttribute []) methodInfo. ReflectedType. GetCustomAttributes (typeof (FilterAttribute), true/* inherit */);

41 FilterAttribute [] methodFilters = (FilterAttribute []) methodInfo. GetCustomAttributes (typeof (FilterAttribute), true/* inherit */);

42 List <FilterAttribute> orderedFilters = typeFilters. Concat (methodFilters). OrderBy (attr => attr. Order). ToList ();

43

44 IList <IFirstFilter> firstFilters = new List <IFirstFilter> ();

45

46 foreach (FilterAttribute filter in orderedFilters)

47 {

48 IFirstFilter castFilter = filter as IFirstFilter;

49 if (castFilter! = Null)

50 {

51 firstFilters. Add (castFilter );

52}

53}

54 return firstFilters;

55}

56

57 private FirstFilterContext InvokeFirstFilters (ControllerContext controllerContext, IList <IFirstFilter> firstFilters, ActionDescriptor actionDescriptor)

58 {

59 FirstFilterContext context = new FirstFilterContext (controllerContext, actionDescriptor );

60 foreach (IFirstFilter filter in firstFilters)

61 {

62 & nbs

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.