Authorization with roles
When an identity is created it may belong to one or more roles. For example, Tracy may belong to the Administrator and User roles whilst Scott may only belong to the User role.
Adding role checks
Role-based authorization checks are declarative—the developer embeds them within their code, against a controller or an action within a controller, specifying roles which the current user must be a member of to access the requested resource.
This controller would be only accessible by users who are members of the HRManager
role or the Finance
role.
If you apply multiple attributes then an accessing user must be a member of all the roles specified:
You can also lock down a controller but allow anonymous, unauthenticated access to individual actions:
Policy based role checks
Role requirements can also be expressed using the new Policy syntax, where a developer registers a policy at startup as part of the Authorization service configuration. This normally occurs in ConfigureServices()
in your Startup.cs file.
Policies are applied using the Policy
property on the AuthorizeAttribute
attribute:
Last updated