C# 我在哪里根据角色和.NET Core controller中的另一个变量实现过滤?

C# 我在哪里根据角色和.NET Core controller中的另一个变量实现过滤?,c#,api,rest,.net-core,C#,Api,Rest,.net Core,我有一个控制器,该控制器具有创建新项的POST端点: [Route("v1/users/{userId:int}/items")] public class ItemsController { private readonly IItemsService _itemsService; public ItemsController(IItemsService itemsService) { _itemsService = itemsSe

我有一个控制器,该控制器具有创建新项的POST端点:

[Route("v1/users/{userId:int}/items")]
public class ItemsController
{
    private readonly IItemsService _itemsService;

    public ItemsController(IItemsService itemsService)
    {
        _itemsService = itemsService;
    }

    [HttpPost]
    public async Task<IActionResult> PostAsync(int userId, [FromBody] ItemModel model)
    {
        var createdModel = await _itemsService.CreateItemAsync(entityId, model);
        var uri = CreateUri(createdModel);

        return Created(uri, createdModel);
    }
}
[路由(“v1/users/{userId:int}/items”)]
公共类项控制器
{
专用只读IItemsService(U itemsService);
公共项控制器(IItemsService itemsService)
{
_itemsService=itemsService;
}
[HttpPost]
公共异步任务PostAsync(int userId,[FromBody]ItemModel)
{
var createdModel=await _itemsService.CreateItemAsync(entityId,model);
var uri=CreateUri(createdModel);
已创建的返回(uri、createdModel);
}
}
我需要做的是禁止基于用户类(从中获取userId)的“Source”属性和调用方角色创建项。例如,如果用户的来源是“Google”,调用方的角色是“NotAdmin”,我想允许创建,但如果用户的来源是“Amazon”,调用方的角色是“NotAdmin”,我想禁止创建

在这种情况下,是否更适合使用ActionFilterAttribute对其进行过滤,在这种情况下,我将调用异步用户服务以让用户访问源属性,然后检查调用者的角色?如果是,这个过滤器是什么样子的

[HttpPost]
[SourceFilterAttribute]
public async Task<IActionResult> PostAsync(int userId, [FromBody] ItemModel model)
{
    var createdModel = await _itemsService.CreateItemAsync(entityId, model);
    var uri = CreateUri(createdModel);

    return Created(uri, createdModel);
}
[HttpPost]
[SourceFilterAttribute]
公共异步任务PostAsync(int userId,[FromBody]ItemModel)
{
var createdModel=await _itemsService.CreateItemAsync(entityId,model);
var uri=CreateUri(createdModel);
已创建的返回(uri、createdModel);
}
或者这个逻辑属于控制器本身吗

[HttpPost]
public async Task<IActionResult> PostAsync(int userId, [FromBody] ItemModel model)
{
    var user = await _userService.GetAsync(userId);
    // Check role and user source here and decide to forbid/allow

    var createdModel = await _itemsService.CreateItemAsync(entityId, model);
    var uri = CreateUri(createdModel);

    return Created(uri, createdModel);
}
[HttpPost]
公共异步任务PostAsync(int userId,[FromBody]ItemModel)
{
var user=await\u userService.GetAsync(userId);
//检查此处的角色和用户源,并决定禁止/允许
var createdModel=await _itemsService.CreateItemAsync(entityId,model);
var uri=CreateUri(createdModel);
已创建的返回(uri、createdModel);
}
还是有更好的解决方案,我没有想到