自定义授权 MVC 3 和 Ninject IoC [英] Custom Authorization MVC 3 and Ninject IoC

查看:25
本文介绍了自定义授权 MVC 3 和 Ninject IoC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的授权类,它继承自 FilterAttribute 并实现了 IAuthorizationFilter.我正在使用最新版本的 Ninject w/asp.net MVC 3 支持.

I have a custom authorization class that inherits from FilterAttribute and implements IAuthorizationFilter. I am using the latest version of Ninject w/ asp.net MVC 3 support.

我遇到的问题是我使用构造函数注入来注入存储库.但是到调用 OnAuthorization 时,存储库为空.这是代码...

The problem I have is I am using constructor injection to inject a repository. But by the the time OnAuthorization is called, the repository is null. Here is the code...

public class MyAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
    {
        private readonly IMyRepo _MyRepo;

        public MyAuthorizeAttribute() { }
        public MyAuthorizeAttribute(IMyRepo myRepo)
        {
            _MyRepo= myRepo; //this gets initialized
        }


        public void OnAuthorization(AuthorizationContext filterContext)
        {
            _MyRepo.DoStuff(); //<< Null, wtf

        }
    }

过滤器绑定:

Bind<IMyRepo>().To<MyRepo>().InRequestScope();


this.BindFilter<MyAuthorizeAttribute >(System.Web.Mvc.FilterScope.Controller, null).WhenControllerHas<MyAuthorizeAttribute >();

更新:我注意到的一件事是此过滤器处于控制器级别.我在操作范围内还有其他过滤器似乎可以正常工作……这可能是原因吗?

Update: One thing I noticed is this filter is at controller level. I have other filters at action scope that seem to work properly...could that be the reason?

更新 2:我已经确认,如果我将过滤器范围更改为 action,则存储库可用 OnAuthorization(非空).

Update 2: I've confirmed that if I change the filter scope to action, then the repository is available OnAuthorization (not null).

这在下面有效,但是我需要在控制器范围内,而不是操作.

This works below, however I need at controller scope, not action.

this.BindFilter<MyAuthorizeAttribute >(System.Web.Mvc.FilterScope.Action, null).WhenActionMethodHas<MyAuthorizeAttribute >();

推荐答案

属性不支持构造函数注入,因为它们是由 .NET Framework 创建的,不受 Ninject 的控制.如果您真的想使用 FilterAttribute(我不推荐),则必须使用属性注入.

Attributes do not support constructor injection as they are created by the .NET Framework and are not under control of Ninject. If you really want to use a FilterAttribute (which I do not recommend) you'll have to use property injection.

而是继续您刚刚开始的工作.您需要一个实现 IAuthorizationFilter 的过滤器(不是从 FilterAttribute 派生的,只需将其从上面的代码中删除),以及一个用于标记控制器/操作的普通属性.

Instead continue what you just began. You need a filter implementing IAuthorizationFilter (not derived from FilterAttribute, just remove it from your code above) and additionally an ordinary attribute to mark the controllers/actions.

然后更改绑定:

this.BindFilter<MyAuthorizeFilter>(FilterScope.Controller, 0).WhenControllerHas<MyAuthorizeAttribute>();

参见:https://github.com/ninject/ninject.web.mvc/wiki/MVC3

您当前实现的问题是它作为过滤器属性被发现一次,并且作为普通过滤器被添加一次.这些实例的一个将注入 repo,另一个实例的 repo 为 null.

The problem with you current implementation is that it is found once as filter attribute and once added as normal filter. One for these instances will have the repo injected an the the repo is null for the other one.

注意:如果这可以简化您的实现,您可以从现有的 FilterAttribute 派生.但是在这种情况下不要将其用作属性,而是将其用作普通过滤器.

NOTE: you can derive from an existing FilterAttribute if this simplifies your implementation. But do not use it as a attribute in this case but use it as an ordinary filter.

这篇关于自定义授权 MVC 3 和 Ninject IoC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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