如何使用Ninject注入服务整合到一个授权过滤器? [英] How to use Ninject to inject services into an authorization filter?
问题描述
我使用asp.net MVC 3,ninject 2.0和ninject MVC 3插件。
I am using asp.net mvc 3, ninject 2.0 and the ninject mvc 3 plugin.
我想知道我怎么得到服务层到我的过滤器(在这种情况下,授权过滤器?)。
I am wondering how do I get service layers into my filter(in this case an authorization filter?).
我喜欢做的构造函数注入所以这是可能的还是我的财产注入?
I like to do constructor inject so is this possible or do I have to property inject?
感谢
修改
我有这种财产注入,但我的财产总是空
I have this for property inject but my property is always null
[Inject]
public IAccountService AccountServiceHelper { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
// check if context is set
if (httpContext == null)
{
throw new ArgumentNullException("httpContext");
}
// check if user is authenticated
if (httpContext.User.Identity.IsAuthenticated == true)
{
// stuff here
return true;
}
return false;
}
/// <summary>
/// Application_Start
/// </summary>
protected void Application_Start()
{
// Hook our DI stuff when application starts
IKernel kernel = SetupDependencyInjection();
RegisterMaps.Register();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public IKernel SetupDependencyInjection()
{
IKernel kernel = CreateKernel();
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
protected IKernel CreateKernel()
{
var modules = new INinjectModule[]
{
new NhibernateModule(),
new ServiceModule(),
new RepoModule()
};
return new StandardKernel(modules);
}
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<IAccountService>().To<AccountService>();
}
}
修改
我升级到2.2 ninject并获得最终得到了它的工作。
I upgraded to ninject 2.2 and get finally got it work.
编辑2
我要尽力为我的授权过滤器做的构造方式,但我不确定如何在角色通过。我猜我不得不通过ninject办呢?
I am going to try and do the constructor way for my authorize filter but I am unsure how to pass in the Roles. I am guessing I have to do it through ninject?
修改3
这是我迄今为止
public class MyAuthorizeAttribute : AuthorizeAttribute
{
private readonly IAccountService accountService;
public MyAuthorizeAttribute(IAccountService accountService)
{
this.accountService = accountService;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
return base.AuthorizeCore(httpContext);
}
}
this.BindFilter<MyAuthorizeAttribute>(FilterScope.Controller, 0)
.WhenControllerHas<MyAuthorizeAttribute>();
[MyAuthorize]
public class MyController : BaseController
{
}
它告诉我希望它是一个没有参数的构造函数。所以我必须失去了一些东西。
It tells me it want's a no parameter constructor. So I must be missing something.
推荐答案
有过滤器的问题是,他们的属性。如果你定义了一些期望依赖你永远不会能够将它应用到任何方法的属性的构造函数:因为你传递给所有属性值必须在编译时已知。
The problem with filters is that they are attributes. And if you define a constructor of an attribute that expects some dependency you will never gonna be able to apply it to any method: because all values that you pass to attributes must be known at compile time.
所以基本上你有两种可能性:
So basically you have two possibilities:
-
使用Ninject应用过滤器,而不是全球的装饰您的控制器/行动吧:
Use Ninject to apply the filter globally instead of decorating your controllers/actions with it:
public interface IFoo { }
public class Foo : IFoo { }
public class MyFooFilter : AuthorizeAttribute
{
public MyFooFilter(IFoo foo)
{
}
}
然后配置内核:
kernel.Bind<IFoo>().To<Foo>();
kernel.BindFilter<MyFooFilter>(FilterScope.Action, 0).When(
(controllerContext, actionDescriptor) =>
string.Equals(
controllerContext.RouteData.GetRequiredString("controller"),
"home",
StringComparison.OrdinalIgnoreCase
)
);
使用属性注:
Use property injection:
public interface IFoo { }
public class Foo : IFoo { }
public class MyFooFilter : AuthorizeAttribute
{
[Inject]
public IFoo Foo { get; set; }
}
然后配置内核:
kernel.Bind<IFoo>().To<Foo>();
和装点一些控制器/动作与您的自定义过滤器:
and decorate some controller/action with your custom filter:
[MyFooFilter]
public ActionResult Index()
{
return View();
}
这篇关于如何使用Ninject注入服务整合到一个授权过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!