在我的 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); …Run Code Online (Sandbox Code Playgroud) c# asp.net asp.net-identity asp.net-core asp.net-core-webapi
如果任务中foreach没有async,那么make ping.Send代替
ping.SendPingAsync那个Task.WhenAll(taskList)就行了.
List<Task> taskList = new List<Task>();
foreach (var host in hostArray)
{
var aHost = host;
Task task = new Task(async ()=>
{
Ping ping = new Ping();
PingResult pingRes = new PingResult { HostNameOrAddress = aHost };
for (int i = 0; i < _pingCount; i++)
{
try
{
PingReply reply = await ping.SendPingAsync(aHost,1000);
if (reply.Status == IPStatus.Success)
pingRes.RoundtripTimes.Add(reply.RoundtripTime);
}
catch
{
// ignored
}
}
Dispatcher.Invoke(() =>
{ …Run Code Online (Sandbox Code Playgroud)