Skip to content

Instantly share code, notes, and snippets.

@mattbajorek
Last active May 10, 2020 02:08
Show Gist options
  • Save mattbajorek/bc293fcf0b37a6e6eaaa1b55b4733e4f to your computer and use it in GitHub Desktop.
Save mattbajorek/bc293fcf0b37a6e6eaaa1b55b4733e4f to your computer and use it in GitHub Desktop.
Policy Based Authorization
...
using Microsoft.AspNetCore.Authorization;
...
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class ClapsController : ControllerBase
{
...
// This policy will only apply to this specific route
[Authorize(Policy = "Admin")]
// GET api/claps/9000
[HttpGet("{id}")]
public ActionResult<int> GetClaps(int id)
{
// This will only return 9000 (ITS OVER 9000) if authenticated and is an Admin
// Otherwise it will return a status code 401
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment