如何执行操作过滤器过滤授权MVC 4日之前 [英] How to execute Action Filter before Authorization Filter MVC 4

查看:101
本文介绍了如何执行操作过滤器过滤授权MVC 4日之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从 AuthorizeAttribute 类继承中实现MVC 4我自己的自定义授权属性。我也有一个自定义的 ActionFilterAttribute 。这些都做工精细但问题的关键在于,命令他们。我需要的自定义操作过滤器自定义过滤器授权之前运行。

I have implemented my own custom Authorization Attribute in MVC 4 by inheriting from AuthorizeAttribute class. I also have a custom ActionFilterAttribute. These both work fine however the problem lies in ordering them. I need the custom Action Filter to run before the custom Authorize Filter.

我已经使用订单属性的属性尝试,但据我所知,授权过滤器总是会操作过滤器之前运行。

I have tried using the Order property for the attributes but as i understand it, Authorize Filters will always run before Action Filters.

有谁知道如何强制行为过滤器来执行前的授权过滤器??

Does anyone know how to force the Action Filter to execute before the Authorize Filter??

推荐答案

当你看看源$ C ​​$ C(可在的 HTTP://aspnetwebstack.$c$cplex.com/ )你看,这是不可能与标准过滤器类。 个IAuthorizationFilter 实现是前 IActionFilter 实现始终执行。这是因为当授权过滤器返回结果的动作过滤器将无法运行。

When you take a look at the source code (available at http://aspnetwebstack.codeplex.com/) you see that this is not possible with the standard Filter classes. IAuthorizationFilter implementations are always executed before IActionFilter implementations. That's because action filters will not run when authorization filters return a result.

要解决这个问题,你可以创建自己的 ControllerActionInvoker 子类,并覆盖 InvokeAction 方法:

To solve this you can create your own ControllerActionInvoker descendant class and override the InvokeAction method:

public class MyControllerActionInvoker : ControllerActionInvoker
{
    public override bool InvokeAction(ControllerContext controllerContext, string actionName)
    {
        // Your initialization code here
        try
        {
            return base.InvokeAction(controllerContext, actionName);
        }
        finally
        {
            // Your finalization code here
        }
    }
}

您需要注入您的自定义 MyControllerActionInvoker 类到你的控制器在自定义的ControllerFactory 类:

You need to inject your custom MyControllerActionInvoker class into your controllers in a custom ControllerFactory class:

public class MyControllerFactory : DefaultControllerFactory
{
    private readonly MyControllerActionInvoker actionInvoker = new MyControllerActionInvoker();

    /// <summary>
    /// Retrieves the controller instance for the specified request context and controller type.
    /// </summary>
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        var controllerInstance = base.GetControllerInstance(requestContext, controllerType);

        if (controllerInstance != null)
        {
            var typedController = controllerInstance as Controller;
            if (typedController != null)
            {
                typedController.ActionInvoker = this.actionInvoker;
            }
        }

        return controllerInstance;
    }
}

当然,你现在的MVC框架,注册自己的 MyControllerFactory 。你应该这样做,你在这里也注册您的路线:

And of course you now have to register your own MyControllerFactory with the MVC framework. You should do this where you're also registering your routes:

var controllerFactory = new MyControllerFactory();
ControllerBuilder.Current.SetControllerFactory(controllerFactory);

本实施工作正常这里。

这篇关于如何执行操作过滤器过滤授权MVC 4日之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆