c# - 如何获取我在 Asp.Net 核心的 Startup.ConfigureServices 中添加的所有授权策略?

标签 c# asp.net asp.net-core asp.net-identity asp.net-core-webapi

在我的Asp.NET Core WebApi应用中,我想给ViewModel中的属性添加权限,然后自定义一个ActionFilter来过滤respone值。如果用户没有权限,该属性将被替换为回调值。 现在,我想使用授权策略来检查权限。

如何获取我在 Startup.ConfigureServices 中添加的所有授权策略?

    public void OnActionExecuted(ActionExecutedContext context)
    {
        if (!(context.Result is JsonResult)) return;
        var c = (JsonResult)context.Result;
        var pas = c.Value
              .GetType().GetTypeInfo()
              .GetProperties()
              .Where(p => p.GetCustomAttribute<PropertyPermissionAttribute>() != null)
              .Select(p =>
                {
                    var attr = p.GetCustomAttribute<PropertyPermissionAttribute>();
                    return (p, attr);
                });

        // ** How to Get All Policies ?? **


        foreach (var (p, a) in pas)
        {
            // Check Policies

            var cb = a.CallbackValue;
            if (cb!=null && p.PropertyType == cb.GetType())
            {
                p.SetValue(c.Value, cb);
            }
            else
            {
                p.SetValue(c.Value, null);
            }
        }
    }

或者是否有任何其他方式在 View 模型的属性中实现权限?

最佳答案

我认为这是不可能的。策略存储在私有(private)字段中。 See source .但是,我不明白为什么您需要所有政策?如果将策略名称存储在 PropertyPermissionAttribute 中,则可以从 IAuthorizationPolicyProvider.GetPolicyAsync(string policyName) 获取必要的策略。

关于c# - 如何获取我在 Asp.Net 核心的 Startup.ConfigureServices 中添加的所有授权策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45014894/

相关文章:

c# - ASP.NET Core 应用程序生命周期

c# - ASP.NET MVC 5 应用程序构建起来真的很慢

c# - 打开自定义文件,args 始终为空

c# - 统一评分系统的创建

c# - 日期之间的季度计数

c# - ASP.NET Core 中的 FromUriAttribute 替换 - 专门用于参数别名?

c# - EndRead引发IO异常

c# - 如何为 Azure 存储表集表服务属性端点构建规范资源?

asp.net - 使用代码 GZipping 进行 IIS 压缩?

c# - 异步任务<IActionResult> 与任务<T>