【发布时间】:2017-12-14 08:32:47
【问题描述】:
在我的 Asp.NET Core WebApi 应用程序中,我想给 ViewModel 中的属性添加权限,然后自定义一个 ActionFilter 来过滤响应值。如果用户没有权限,该属性将被一个回调值替换。 现在,我想使用授权策略来检查权限。
如何获取我在 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);
}
}
}
或者是否有任何其他方式在 viewmodel 的属性中实现权限?
【问题讨论】:
标签: c# asp.net asp.net-core asp.net-identity asp.net-core-webapi