简体   繁体   English

在 ActionFilterAttribute 中查找过滤器

[英]Finding a filter in ActionFilterAttribute

I created a custom ActionFilterAttribute which I call like this:我创建了一个自定义 ActionFilterAttribute,我这样称呼它:

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

Now, how do I find the value of acceptedScopes in the OnActionExecuting method?现在,如何在OnActionExecuting方法中找到acceptedScopes范围的值? And how do I check that acceptedScopes was passed to the ActionFilter?以及如何检查是否已将接受范围传递给 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[]);

        }    
    }
string[] ActionArguments = ((ScopeActionFilter)actionContext.Filters.Where(t => t is ScopeActionFilter).First()).acceptedScopes;

will work将工作

how do I find the value of acceptedScopes in the OnActionExecuting method?如何在 OnActionExecuting 方法中找到接受范围的值?

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:在您的代码中,我们可以发现您在将ScopeActionFilter应用于 action 方法时设置了acceptedScopes属性的值,要在OnActionExecuting 方法中获取acceptedScopes的值,您可以尝试:

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测试结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM