Ninject绑定属性与构造函数参数筛选 [英] Ninject Binding Attribute to Filter with Constructor Arguments
问题描述
我看了尽可能多的答案,因为我能为这一点,但他们似乎功亏一篑一个细节的。
I read as many answers as I could for this, but they seem to fall short of one detail.
麻烦的是绑定的动作过滤器(由控制器注入了服务)到相应的属性,我一直无法弄清楚如何从属性到绑定的过滤器通过参数/属性值时。下面是code和低于预期的我fake- code:
The trouble is when binding an action filter (with a service injected by controller) to a corresponding attribute, I've been unable to figure out how to pass parameter/property values from the attribute to its bound filter. Below is the code, and below that my intended fake-code:
过滤器和放大器;属性
public class AuthorizationFilter : IAuthorizationFilter
{
private readonly IAuthorizationService _authorizationService;
private readonly UserRoles _requiredRoles; // Enum
public AuthorizationFilter(IAuthorizationService authorizationService, UserRoles requiredRoles)
{
_authorizationService = authorizationService;
_requiredRoles = requiredRoles;
}
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Session == null)
HandleUnauthorizedRequest(filterContext);
else {
var authorized = _authorizationService.IsUserInRole((UserSessionInfoViewModel) filterContext.HttpContext.Session["user"], _requiredRoles);
if (!authorized)
HandleUnauthorizedRequest(filterContext);
// else TODO: deal with cache...
}
}
}
public class RequireRolesAttribute : FilterAttribute
{
public readonly UserRoles RequiredRoles;
public RequireRolesAttribute(UserRoles requiredRoles)
{
RequiredRoles = requiredRoles;
}
}
过滤器/属性绑定
kernel.BindFilter<AuthorizationFilter>(FilterScope.Controller, 0)
.WhenControllerHas<RequireRolesAttribute>();
kernel.BindFilter<AuthorizationFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<RequireRolesAttribute>();
这应该确保任何控制器/动作饰[RolesRequired]绑定到过滤器。到现在为止还挺好。现在我想通过属性来声明的角色(很像股票AuthorizeAttribute),并通过这些值到实际执行授权的过滤器。
This should make sure any controller/action decorated with [RolesRequired] is bound to the filter. So far so good. Now I want to declare via the attribute the roles (much like the stock AuthorizeAttribute) and pass those values onto the filter that actually does the authorization.
拟/假code:
[RequireRoles(UserRoles.Author)]
public ActionResult Index()
{
// blah
}
具体来说,
怎样才能告知角色的AuthorizationFilter?可以过滤/ ninject访问传递给属性的构造函数的参数?可以过滤/ ninject从属性的公共财产拉他们?
What does it take to inform the AuthorizationFilter of the roles? Can the filter/ninject access the arguments passed to the attribute's constructor? Can the filter/ninject pull them from the attributes public property?
有关参考,这些文章是一个巨大的帮助,但不回答这样一件事:
For reference, these articles were a huge help, but don't answer this one thing:
<一个href=\"http://stackoverflow.com/questions/6193414/dependency-injection-with-ninject-and-filter-attribute-for-asp-net-mvc\">Dependency对于asp.net注射Ninject和Filter属性MVC
<一个href=\"http://stackoverflow.com/questions/5572257/custom-authorization-mvc-3-and-ninject-ioc/5572752#5572752\">Custom授权MVC 3和Ninject的IoC
乙Z,雷莫Gloor,别人...我怎样才能做到这一点?
B Z, Remo Gloor, others... how can I accomplish this?
推荐答案
我已经想通了(感谢圣雷莫的指示和文档)。
I have figured it out (thanks to Remo's directions and documentation).
使用适当的 .WithConstructorArgument 扩展是否要绑定到一个控制器或者动作过滤器。例如结合我的动作过滤器是这样的:
Use the appropriate .WithConstructorArgument extension whether you are binding to a Controller or Action filter. For example binding my action filter looks like this:
kernel.BindFilter<AuthorizationFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<RequireRolesAttribute>()
.WithConstructorArgumentFromActionAttribute<RequireRolesAttribute>("requiredRoles", o => o.RequiredRoles);
在我理解的 Func键&LT;> 签名,这一切都变得清晰。我发现处理这种情况最好的办法就是
Once I understood the Func<> signature, it all became clear. The best way I found to handle this was to
-
请扩展型特异性为我的属性。
make the extension type-specific for my attribute
.WithConstructorArgumentFromActionAttribute<TAttribute>()
从获取通过拉姆达回调对象(您的属性)值:
fetch the value from the callback object (your attribute) via lambda:
("argumentName", o => o.PropertyName)
这篇关于Ninject绑定属性与构造函数参数筛选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!