I am trying to set up action filter with attribute in ASP.NET Web api.
Here is the filter
public class PermissionRequirementFilter: IAuthorizationFilter
{
readonly string[] _permissions;
public PermissionRequirementFilter(string[] permissions)
{
_permissions = permissions;
}
public void OnAuthorization(AuthorizationFilterContext context)
{
var claim = context.HttpContext.User.Claims;
var service = context.HttpContext.RequestServices.GetService<IntegrationContext>() ?? throw new InvalidOperationException();
var roleClaim = claim.FirstOrDefault(c => c.Type == "extension_Role");
//TODO Rest of logic here
}
Here is the attribute
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public class RequiredPermissionAttribute : TypeFilterAttribute
{
public RequiredPermissionAttribute(string[] permissions) : base(typeof(PermissionRequirementFilter))
{
Arguments = // Not sure what to do here...
}
}
Here is how i would like to use this in the controller.
[ApiController]
[Route("[controller]")]
[EnableCors("AllowAll")]
public class AbcController : ControllerBase
{
[HttpGet]
[RequiredPermission(new []{"permission_1", "permission_2"})]
[Authorize]
public async Task<ActionResult> Get()
{
return Ok("test");
}
}
I have tried variations of this but i can never seem to get the code to hit the filter without registering it (cant use the data from attribute then)
Anyone have any ideas?