Asp.net mvc 是否可以只覆盖一个授权筛选器?

Asp.net mvc 是否可以只覆盖一个授权筛选器?,asp.net-mvc,asp.net-mvc-5,asp.net-mvc-5.2,Asp.net Mvc,Asp.net Mvc 5,Asp.net Mvc 5.2,我遇到了一种情况,即我使用多个授权过滤器来强制执行彼此独立的不同条件: <UserLevelCanAccessThings> <UserHasPaidForThings> Partial Public Class ThingsController Inherits Controller Function EditThing() As ActionResult ... Return View() End Functi

我遇到了一种情况,即我使用多个授权过滤器来强制执行彼此独立的不同条件:

<UserLevelCanAccessThings>
<UserHasPaidForThings>
Partial Public Class ThingsController
    Inherits Controller

    Function EditThing() As ActionResult
        ...
        Return View()
    End Function

    <OverrideUserIsPaid>
    Function PayToUnlockThings() As ActionResult
        ...
        Return View()
    End Function

End Class

部分公共类ThingsController
继承控制器
函数EditThing()作为ActionResult
...
返回视图()
端函数
函数PayToUnlockThings()作为ActionResult
...
返回视图()
端函数
末级
现在我遇到了一个情况,我需要一个动作绕过一个过滤器。例如,假设您已达到足够高的级别(第一个过滤器),并且您已为该访问付费(第二个过滤器),则允许您访问该控制器以处理
事物

Pay方法需要强制执行第一个条件,而不是第二个条件,因为我不想阻止用户付款,因为当用户尝试支付时,他没有支付

(我知道我可以将薪酬方法转移到其他地方,但在我的实际情况下,这是不可取的。这只是一个例子来说明我的要求。)

全局
OverrideAuthorization
属性关闭所有授权,但我需要对其进行更多选择。我怎样才能做到这一点呢?

接下来,我做到了:

Public Class OverrideUserIsPaidAttribute
    Inherits AuthorizeAttribute

    Public Overrides Sub OnAuthorization(filterContext As AuthorizationContext)
        'no op
    End Sub
End Class

Public Class UserIsPaidAttribute
    Inherits AuthorizeAttribute

    Public Overrides Sub OnAuthorization(filterContext As AuthorizationContext)
        'get all other filters attached to this action, controller, or globally
        Dim otherFilters = filterContext.ActionDescriptor.GetCustomAttributes(True).AsEnumerable '.Where(Function(x) TypeOf x Is IAuthorizationFilter And Not TypeOf x Is OverridePlayerIsPaidAttribute).AsEnumerable(Of IAuthorizationFilter)()
        otherFilters = otherFilters.Union(filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(True))
        otherFilters = otherFilters.Union(GlobalFilters.Filters.Select(Function(x) x.Instance))

        'if any of them is an override, bail out before validating
        If otherFilters.OfType(Of UserIsPaidAttribute).Count > 0 Then Return

        'regular validation here
    End Sub
基本上,它增强了
UserIsPaid
授权,以检查操作、控制器和全局过滤器的属性,以查看是否指定了覆盖,如果是,则不执行自己的授权。这对我来说似乎足够干净,但我愿意接受建议。

您可以检查一下:但听起来您想要做的事情可能应该以不同的方式设计。可能是声明或其他角色或权限范例?