检查被请求者是否可以访问另一条路径C#DotNet Core

时间:2017-10-06 15:38:54

标签: c# asp.net-core asp.net-core-mvc asp.net-core-2.0

我想在我的C#Web Api中使用Hateoas。如果被请求者通过提供路由名称可以访问其他路由,我希望能够解决。

  1. 当被请求者呼叫/ A并且通过PolicyB而不是PolicyC时,我希望他们接收“获取”,而不是“发布”
  2. 当被请求者呼叫/ A并通过PolicyBPolicyC时,我希望他们接收“获取”和“发布”回来
  3. 以下是示例代码:

    public class AController : Controller
    {
        [HttpGet("A", Name = "RouteA")]
        public IActionResult GetA()
        {
            IEnumerable<string> routesAbleToAccess = MethodsUserCanAccessForRoute("RouteB");
            return Ok(routesAbleToAccess);
        }
    
        [HttpGet("B", Name = "RouteB")]
        [Authorize("PolicyB")]
        public IActionResult GetB()
        {
            return Ok();
        }
    
    
        [HttpPost("C")] // Also named Route B
        [Authorize("PolicyC")]
        public IActionResult SaveC()
        {
            return Ok();
        }
    
        private IEnumerable<string> MethodsUserCanAccessForRoute(string route)
        {
            // logic to get the routes that the user can access
        }
    }
    

0 个答案:

没有答案