动作过滤器执行顺序

发布于 2024-12-12 05:48:27 字数 609 浏览 0 评论 0原文

我创建了两个实现 AuthorizeAttribute 的类。

一个是全局使用的,我在 Global.asax.cs 上设置它:

filters.Add(new FirstAuthorizeAttribute() { Order = 0 });

另一个称为 SecondAuthorizeAttribute ,它仅在某些操作方法中使用,我将它用作我想要的方法中的属性。

    [HttpGet]
    [SecondAuthorize]
    public ActionResult LogOut()
    {
        FormsAuthentication.SignOut();
        Session.Clear();
        Session.Abandon();
        return Redirect(Url.Content("~/"));
    }

问题是 SecondAuthorizeAttribute 总是在 FirstAuthorizeAttribute 之前执行,而我需要先执行这个。该命令没有帮助,我该怎么办?

I have created two classes that implement AuthorizeAttribute.

One is used globally, and I set it on the Global.asax.cs:

filters.Add(new FirstAuthorizeAttribute() { Order = 0 });

The other is called SecondAuthorizeAttribute and it is used only in some action methods, and I use it as attribute in the methods I want.

    [HttpGet]
    [SecondAuthorize]
    public ActionResult LogOut()
    {
        FormsAuthentication.SignOut();
        Session.Clear();
        Session.Abandon();
        return Redirect(Url.Content("~/"));
    }

The problem is that SecondAuthorizeAttribute always execute before FirstAuthorizeAttribute, and I need this one to execute first. The order is not being helpful, how could I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小矜持 2024-12-19 05:48:27

@HectorCorrea 的答案中的链接目前已失效,以下是从当前 Google 缓存中检索和总结的内容(如果也出现这种情况):

过滤器按以下顺序执行:

  • 授权过滤器
  • 操作过滤器
  • 响应/结果过滤器
  • 异常过滤器

每个过滤器内,您可以指定 Order 属性。 (所有过滤器均派生自抽象类 FilterAttribute,并且该类具有 Order 属性)。此属性将确保过滤器按特定顺序运行。

例如:

[AuthorizationFilterA(Order=2)]
[AuthorizationFilterB(Order=1)]
public ActionResult Index()
{          
    return View();
}

还有 FilterScope,默认情况下,当顺序相同(或未指定)时,范围最低的过滤器首先运行:

namespace System.Web.Mvc {
    public enum FilterScope {
        First = 0,
        Global = 10,
        Controller = 20,
        Action = 30,
        Last = 100,
    }
}

如果未指定顺序,则顺序值为 -1 (第一个,不是最后一个)。

控制器本身可以是过滤器,并将按 Int32.MinValue 的顺序运行

The link in @HectorCorrea's answer is dead at the moment, here's the content retrieved and summarised from the current Google cache (in case that also goes) :

Filters execute in this order:

  • Authorization filters
  • Action filters
  • Response/Result filters
  • Exception filters

Within each filter, you may specify the Order property. (All filters are derived from the abstract class FilterAttribute, and this class has an Order property). This property will ensure the filter runs in a specific Order.

eg:

[AuthorizationFilterA(Order=2)]
[AuthorizationFilterB(Order=1)]
public ActionResult Index()
{          
    return View();
}

There's also FilterScope and, by default, the filter with the lowest scope runs first when the order is the same (or not specified):

namespace System.Web.Mvc {
    public enum FilterScope {
        First = 0,
        Global = 10,
        Controller = 20,
        Action = 30,
        Last = 100,
    }
}

If no order is specified, the order value is -1 (first, not last).

Controllers themselves can be filters and will run with order Int32.MinValue

拥有 2024-12-19 05:48:27

这是一个漫长的过程,但是您是否尝试过使用 FirstAuthorizeAttribute 的 Global 和 First 值?

http://msdn.microsoft.com/en -us/library/system.web.mvc.filterscope(v=vs.98).aspx

http://blog.rajsoftware.com/post/2011/05/14/MVC3-过滤器排序.aspx

This is a long shot, but have you tried using the Global and First values for your FirstAuthorizeAttribute ?

http://msdn.microsoft.com/en-us/library/system.web.mvc.filterscope(v=vs.98).aspx

http://blog.rajsoftware.com/post/2011/05/14/MVC3-Filter-Ordering.aspx

妄断弥空 2024-12-19 05:48:27

订单在 global.asax.cs 文件中不起作用。
如果要求是订单,则转到控制器或操作方法并给出。
例如:-

[第二个属性(顺序=1)]
[firstAttribute(order=2)]

HomeController:控制器

公共 ActionResult Index()

Order is not working in global.asax.cs file.
If requirement is order then goto Controller or Action Method and give.
ex:-

[secondAttribute(order =1)]
[firstAttribute(order=2)]

HomeController:Controller

or

public ActionResult Index()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文