asp.net-mvc-4 - Ninject 授权与操作过滤器绑定(bind)顺序

标签 asp.net-mvc-4 ninject action-filter authorize-attribute custom-action-filter

我尝试使用 Ninject 的 BindFilter<> 应用 2 个过滤器语法,并且它们正在成功应用依赖注入(inject)。问题在于,它会找出当前用户是谁并绑定(bind)InRequestScope。 ,并且在检查维护授权时必须在第二个之前运行 - 否则它不知道您指的是哪个用户。

NinjectWebCommon.cs 中的过滤器绑定(bind)看起来像这样:

kernel.BindFilter<CurrentUserFilter>(FilterScope.Global, 0).InRequestScope();
kernel.BindFilter<SetupRightsAttribute>(FilterScope.Controller, 1).WhenControllerType<MaintenanceController>().InRequestScope();

因此,在维护 Controller 中,我希望运行第一个 Controller ,然后运行第二个 Controller ;在其他所有内容中,只有第一个 - 这有效,第二个仅在浏览到我的维护 Controller 中的某些内容时使用。

这 2 个过滤器是这样声明的(排除不相关的细节)

public class CurrentUserFilter : IActionFilter
{
    public void OnActionExecuting(ActionExecutingContext filterContext)
    // implementation - breakpoint 1
}

public class SetupRightsAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    // implementation - breakpoint 2
}

我预计断点 1 会被击中,然后是断点 2,但出于某种原因,它总是首先出现断点 2 - 并且因为它此时不知道用户,所以它告诉我我没有授权。

我尝试了 FilterScope 和顺序设置的许多不同排列,甚至以不同的顺序绑定(bind)它们,但没有任何效果......我做错了什么?

最佳答案

授权过滤器在操作过滤器之前执行,因为授权在 MVC 执行管道中比处理操作更早。

关于asp.net-mvc-4 - Ninject 授权与操作过滤器绑定(bind)顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13154697/

相关文章:

c# - 在您的项目中是否有任何您认为必不可少的 Action 过滤器?

.net - SimpleMembership 会取消用户身份验证?

asp.net-mvc - 当实体位于使用 MVC 或 Web API 的另一层时,Models 文件夹将为空

c# - 使用 Entity Framework 将 SQL Server 数据库中的表拉入 C# 中的 List<>

asp.net-mvc-3 - 使用 MVC 3 依赖注入(inject)和 Ninject 2.2 将全局操作过滤器绑定(bind)到区域中的所有 Controller

asp.net-mvc - 为多语言 ASP.NET MVC Web 应用程序设置 CurrentCulture 的最佳位置

c# - Entity Framework 。依赖角色有多个具有不同值的主体

c# - Ninject 对 MVC3 中 Action 方法参数的依赖注入(inject)

c# - 使用 NUnit 在不同的应用程序域中运行单元测试

asp.net-mvc - 使用 MSpec 测试 ActionFilterAttributes