无法从实现的应用方法的 IActionModelConvention 中的授权属性访问角色

Unable to access the Roles from Authorize Attribute in IActionModelConvention implemented Apply Method

我在这里尝试从应用授权过滤器的操作方法中解析角色。但我只能通过快速观看进入运行时。但无法通过 LINQ 查询获取它。

internal class AuthorizeModelConvention : IActionModelConvention
    { 
        public void Apply(ActionModel action)
        {
            IReadOnlyList<object> actionAttributes = action.Attributes;

            if (!actionAttributes.Select(x => x.GetType()).Any(x => x.Name.Contains("AllowAnonymousAttribute")))
            { 
                action.Filters.Add(new AuthorizeFilter("auth-policy")); 
            }
        }
    }

根据您的描述,我建议您可以尝试使用以下代码来检查授权属性的角色值是否为“admin”。

 actionAttributes.Select(t => t.GetType().GetProperties().ToList().Select(x => x.GetValue(t, null)).ToList()).Any(x => x.Contains("admin"));

结果: