I want to use Hateoas in my C# Web Api. I want to be able to work out if the requestee has access to other routes by providing the route name.
- When the requestee calls /A and would pass
PolicyB
but notPolicyC
, I want them to receive "Get" back, not "Post" - When the requestee calls /A and would pass
PolicyB
andPolicyC
, I want them to receive "Get" and "Post" back
Here is the example code:
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
}
}