I have implemented the ClaimRequirementFilter, but the injection/ parameter handling doesn't work. The constructor of the AuthorizationFilter is called twice. The MyClaim object (parameter) Name property is null during the first call and in the second it is filled. The OnAuthorization is only called once, using the null value.
MyClaimAttribute.cs
public class MyClaimAttribute : TypeFilterAttribute
{
public MyClaimAttribute(string claimName) : base(typeof(AuthorizationFilter))
{
MyClaim claim = new MyClaim
{
Name = claimName
};
Arguments = new object[] { claim };
}
}
AuthorizationFilter.cs
public AuthorizationFilter(MyClaim claim)
{
_claim = claim;
}
Startup.cs
services.AddTransient<MyClaim>();
services.AddMvc(options => options.Filters.Add(typeof(AuthorizationFilter)));
Controller method is decorated with: [MyClaim("CanDoSomething")]
Update 1:
Removing the 'services.AddMvc' line fixed the issue, but I would like to execute it for every action. If one of the methods isn't decorated, the user shouldn't be authorized.