ASP.NET MVC 5中的Actionfilter注入 [英] Actionfilter Injection in ASP.NET MVC 5
问题描述
我有一个简单的过滤器.
I have a simple filter.
public class IsAdmin : ActionFilterAttribute, IAuthenticationFilter
{
private string _roleName;
IBusinessIdentity _identity;
public IsAdmin(string roleName, IBusinessIdentity identity)
{
this._roleName = roleName;
this._identity = identity;
}
public void OnAuthentication(AuthenticationContext filterContext)
{
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
if (!_identity.Roles.Contains(_roleName))
filterContext.Result = new HttpUnauthorizedResult();
}
}
我正在使用Ninject.这是我的控制器.我试图将注入的服务放入我的ActionFilter中,以便不依赖于HttpContext
而是依赖于我的IBusinessIdentity
.
I am using Ninject. Here is my controller. I'm trying to get the injected service into my ActionFilter in order not to take a dependency on HttpContext
but instead on my IBusinessIdentity
.
IBusinessIdentity
被注入HttpContext.User.Identity`.并执行一些数据库调用并获取userRoles.
IBusinessIdentity
gets injected the HttpContext.User.Identity`. And it does a few database calls and gets the userRoles.
public class HomeController : Controller
{
readonly IBusinessIdentity _identity;
public HomeController(IBusinessIdentity identity)
{
this._identity= identity;
}
[IsAdmin("Admin", _identity)]
public ActionResult Index()
{
return View();
}
}
这不起作用,尝试在编译时将"identity"放入actionfilter构造函数中时出现编译器错误.
This does not work and I'm getting a compiler error when I try to put the "identity" in the actionfilter constructor at compile time.
非静态字段,方法或属性需要对象引用
An object reference is required for the non-static field, method, or property
我需要这个,因为我打算用该身份测试各种权限.
I need this because I'm planning to test various permissions with the identity.
我正在考虑在实例化控制器后进行某种反思.我对如何实现有一个非常模糊的想法.
I am thinking some kind of reflection to be done after controllers gets instantiated. I have a very vague idea on how it could be done.
我正在使用ASP.NET MVC 5,但是没有kernel.bindfilter.我不能使用旧版本.
I am using ASP.NET MVC 5 and I don't have the kernel.bindfilter. I can't use older version.
我很清楚这种黑客攻击.
I am well aware of this hack.
https://github.com/ninject /Ninject.Web.Mvc/wiki/Conditional-bindings-for-filters
使用Ninject for MVC 5如何实现相同的效果.
How can I accomplish the same maybe effect using Ninject for MVC 5.
编辑:大规模失败
我忘了包括:
using Ninject.Web.Mvc.FilterBindingSyntax;
现在一切正常,如上面的链接所述.
Now everything works as explained in the above links.
现在,我需要弄清楚如何在过滤器构造函数中注入"roleName"字符串.虽然我认为只是为每个角色构造一个筛选器.稍后我将发布整个代码.
Now I need to figure out how to inject the "roleName" string in the filter constructor. Although I think just construct a filter for every role. I will post entire code later.
推荐答案
Although your question is different, the answer is exactly the same as this one.
DI友好属性不应定义任何行为.您需要将行为分离到一个单独的过滤器中,该过滤器可以在应用程序启动时注入其依赖项.这可以通过将动作过滤器属性分为两部分来完成.
DI friendly attributes should never define any behavior. You need to separate the behavior out into a separate filter that can have its dependencies injected at application startup. This can be done by splitting your action filter attribute into 2 parts.
- 一个不包含任何行为来标记您的控制器和操作方法的属性.
- 一个DI友好类,实现了 IActionFilter 和/或
- An attribute that contains no behavior to flag your controllers and action methods with.
- A DI-friendly class that implements IActionFilter and/or IAuthenticationFilter that contains the desired behavior with a scanning implementation to check for the attribute.
不要让Microsoft的ActionFilterAttribute营销欺骗您.这种方法完全与DI相抵触.
Don't let Microsoft's marketing of ActionFilterAttribute fool you. That approach is completely hostile to DI.
这篇关于ASP.NET MVC 5中的Actionfilter注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!