[英]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.