【问题标题】:Unable to access the Roles from Authorize Attribute in IActionModelConvention implemented Apply Method无法从 IActionModelConvention 实施的应用方法中的授权属性访问角色
【发布时间】:2020-10-26 06:36:21
【问题描述】:

我在这里尝试从 Action Method Applied Authorize 过滤器中解析角色。但我只能通过快速观看进入运行时。但无法通过 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")); 
            }
        }
    }

【问题讨论】:

  • 有什么更新吗?我的回复能解决你的问题吗?

标签: asp.net asp.net-core authentication oauth


【解决方案1】:

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

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

结果:

【讨论】:

    猜你喜欢
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多