Search code examples
c#asp.net-coreactionfilterattribute

Finding a filter in ActionFilterAttribute


I created a custom ActionFilterAttribute which I call like this :

 [ScopeActionFilter(acceptedScopes = new string[] { "Files.Upload" })]
  public IActionResult Upload(IFormFile[] files)
   {
   }

Now, how do I find the value of acceptedScopes in the OnActionExecuting method ? And how do I check that acceptedScopes was passed to the ActionFilter ?

 public class ScopeActionFilter : ActionFilterAttribute
    {
        public string[] acceptedScopes { get; set; }

        public override void OnActionExecuting(ActionExecutingContext actionContext)
        { 
       
                ScopesRequiredByWebApiExtension.VerifyUserHasAnyAcceptedScope(actionContext.HttpContext, actionContext.ActionArguments["acceptedScopes"] as string[]);

        }    
    }

Solution

  • how do I find the value of acceptedScopes in the OnActionExecuting method ?

    In your code, we can find that you set the value for acceptedScopes property while you applying the ScopeActionFilter to action method, to get the value of acceptedScopes in the OnActionExecuting method, you can try:

    public class ScopeActionFilter : ActionFilterAttribute
    {
        public string[] acceptedScopes { get; set; }
    
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var args = acceptedScopes;
       
            ScopesRequiredByWebApiExtension.VerifyUserHasAnyAcceptedScope(actionContext.HttpContext, args);
    
        }
    } 
    

    Test Result

    enter image description here