How to get all Authorize Policies that I added in the Startup.ConfigureServices in Asp.Net core?

Viewed 1684

In my Asp.NET Core WebApi application, I want to add permissions to the properties in ViewModel, and then customize an ActionFilter to filter the respone value. If the user does not have the permission, the property will be replaced by a callback value. Now, I want to use Authorize Policy to check the permission.

How to get all Authorize Policies that I added in the 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);
            }
        }
    }

Or is there any other ways to implement permissions at viewmodel's properties?

1 Answers
Related