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

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

问题描述

我有一个从FilterAttribute继承并实现个IAuthorizationFilter定制的授权类。我使用的是最新版本的Ninject的W / asp.net MVC 3支持。

我的问题是我使用构造器注入注入的存储库。但是到了时间OnAuthorization被调用时,库为空。这里是code ...

 公共类MyAuthorizeAttribute:FilterAttribute,个IAuthorizationFilter
    {
        私人只读IMyRepo _MyRepo;        公共MyAuthorizeAttribute(){}
        公共MyAuthorizeAttribute(IMyRepo myRepo)
        {
            _MyRepo = myRepo; //这个被初始化
        }
        公共无效OnAuthorization(AuthorizationContext filterContext)
        {
            _MyRepo.DoStuff(); //<<空,WTF        }
    }

过滤器绑定:

 绑定< IMyRepo>()到<&MyRepo GT;()InRequestScope()。
this.BindFilter< MyAuthorizeAttribute>(System.Web.Mvc.FilterScope.Controller,NULL).WhenControllerHas< MyAuthorizeAttribute>();

更新:
有一件事我注意到的是这个过滤器是在控制器级别。我有行动的范围,似乎正常工作其他过滤器......会是什么原因呢?

更新2:
我已经证实,如果我更改筛选范围的行动,则存储库可OnAuthorization(NOT NULL)。

这下面的工作,但是我需要控制范围,而不是采取行动。

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


解决方案

属性不支持构造函数注入,因为它们是由.NET Framework创建的,不是在Ninject的控制权。如果你真的想使用FilterAttribute(我不推荐),你将不得不使用属性注入。

而是继续自己刚才开始。你需要实现个IAuthorizationFilter过滤器(不是从FilterAttribute衍生,只是从code以上将其删除),另外一个普通的属性来标记的控制器/行动。

然后更改绑定:

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

请参阅:<一href=\"https://github.com/ninject/ninject.web.mvc/wiki/MVC3\">https://github.com/ninject/ninject.web.mvc/wiki/MVC3

与您当前的实现的问题是,它是作为过滤器属性发现一次,一次添加作为正常滤波器。一个用于这些实例将回购注入的的回购是空的另外一个。

请注意:您可以从现有的FilterAttribute得出,如果这可以简化您的实现。但不要把它作为在这种情况下属性,但是用它作为一个普通的过滤器。

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.

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

        }
    }

Filter Binding:

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?

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 >();

解决方案

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.

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.

Then change the binding:

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

See: https://github.com/ninject/ninject.web.mvc/wiki/MVC3

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.

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天全站免登陆