I'd like to pass varying number of parameters to an ActionFilter. example:
[TypeFilter(typeof(AuthorizeFilter), Arguments = new object[] {PolicyName.CanUpdateModule, PolicyName.CanReadModule })]
public async Task<IActionResult> PutModule([FromRoute] Guid id, [FromBody] Module module)
I defined filter like below and I get the error "InvalidOperationException: A suitable constructor for type 'MyApp.AuthFilters.AuthorizeFilter' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.". How do I get around this issue?
public class AuthorizeFilter : ActionFilterAttribute
{
private readonly IAuthorizationService _authService;
private readonly string[] _policyNames;
public AuthorizeFilter(IAuthorizationService authService,params string[] policyNames)
{
_authService = authService;
_policyNames = policyNames.Select(f => f.ToString()).ToArray();
}...
}
Close but no cigar. You are calling your filter with the wrong parameters. You already are calling it as a TypeFilterAttribute
, since you need DI and arguments passed in.
Now you just need to fix your arguments. You want to pass in a string array, but you pass in several strings.
[TypeFilter(typeof(AuthorizeFilter),
Arguments = new object[] {
new string[] { PolicyName.CanUpdateModule, PolicyName.CanReadModule }
}
)]
public async Task<IActionResult> PutModule([FromRoute] Guid id, [FromBody] Module module) {
/*do stuff*/
}
Nontheless, your IAuthorizationService
still needs to be registered with your DI container, to be resolved.
Then you need to remove your params
keyword from your AuthorizeFilter
class:
public class AuthorizeFilter : ActionFilterAttribute
{
private readonly IAuthorizationService _authService;
private readonly string[] _policyNames;
public AuthorizeFilter(IAuthorizationService authService,string[] policyNames)
{
_authService = authService;
_policyNames = policyNames;
}
/* ... */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With