Implementing fine-grained permissions in ASP.NET Core 3

Viewed 384

I'd like to provide fine-grained permissions for my new ASP.NET Core 3 app. There will be a lot of areas in the application (a couple at start, but every new feature will go with another area to control access to) and I suspect that sooner or later I'll need precise access control (eg. this user should have access to what Employee can PLUS areas X, Y RW and area Z RO).

Implementing such mechanism by myself is easy, but I'd like to use ASP's builtin mechanisms not to write everything from scratch. So basing on what I've read about ASP.NET Core's authorization mechanisms, I'd go the following way:

  • Provide Claims for users, which would control access to a specific area via flag (say, List, Read, Write, Delete, Admin)
  • Create implementation of IAuthorizationPolicyProvider, which would create specific policies on the fly, basing on parameter passed, say: [Authorize(Policy = "Area1,r")] or [Authorize(Policy = "Area5,a")] (using handlers created on the fly)
  • Register my custom IAuthorizationPolicyProvider with the dependency injection system

To be honest, this seems like a little bit of overengineering for me, as I could implement my own authorization attribute, which - for starters - would simplify entering restrictions (to the point, that errors in access definition would be caught during compilation, not during application running):

[RestrictAccess(Areas.Area1, AreaAccess.Read)]
public IActionResult MyAction() { }

However, this seems to be a solution, which evades using ASP.NET Core mechanisms, because the one I've presented earlier seems to be the way Microsoft wants us to use policies (see Custom Authorization Policy)

TL;DR: What is the proper way of implementing such fine-grained permissions in ASP.Net Core 3? And furthermore, what are the actual advantages of implementing permissions the way Microsoft suggests?

0 Answers
Related