I've hit a bit of a wall and was hoping someone could point out where I'm going wrong.
I've been using Ninject to Inject into custom ActionFilterAttributes and this is working fine:
kernel.BindFilter<CriticalErrorAttribute>(FilterScope.Last, 1);
I'm now trying to inject into a custom AuthorizeAttribute. I've got the syntax correct so that I'm inserting the Role and custom attributes:
kernel.BindFilter<Authorisation>(FilterScope.Action, 0)
.WhenActionMethodHas<Authorisation>()
.WithPropertyValueFromActionAttribute<Authorisation>("Roles", n => n.Roles)
.WithPropertyValueFromActionAttribute<Authorisation>("Years", n => n.Years);
The attribute is being executed correctly and the roles and years are being inserted fine, my issue is that a service I'm trying to inject in is always null:
[Inject]
public IUserServices userService { get; set; }
In normal ActionFilterAttributes the service is injected fine, but here it isn't.
Any help would be appreciated
Instead of deriving from an attribute you should implement the corresponding interface e.g. IAuthorizationFilter
or IActionFilter
and use a different normal attribut to mark the controllers or actions for which you want to apply that filter
public class AuthorisationFilter : IAuthorizationFilter ....
public class Authorization : Attribute ....
kernel.BindFilter<AuthorisationFilter>(FilterScope.Action, 0)
.WhenActionMethodHas<Authorisation>()
.WithPropertyValueFromActionAttribute<Authorisation>("Roles", n => n.Roles)
.WithPropertyValueFromActionAttribute<Authorisation>("Years", n => n.Years);