dotnet / dotnet/aspnetcore

Allow combining authorization policies in Authorize attribute same way how it works with roles.

Open
#27,061 8 comments 3 reactions 0 assignees View on GitHub
area-auth
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

`Authorize` attribute allows to specify multiple roles which work with OR-semantics. Unfortunately, there's only `Policy` property rather than `Policies`.

Policy defines requirements with AND-semantics. OR-semantics for single requirement can be implemented with multiple handlers. However, there's no option to easily combine policies in OR-mode.

For example, we want to check that user is Administrator, or resource belongs to the user. I see these checks as two distinct policies having various requirements:

```CSharp
[Authorize(Policies="UserIsAdministrator, UserOwnsTheOrder"]
[ApiController]
MyController : Controller {}
```

```CSharp
services.AddAuthorization(cfg => {
cfg.AddPolicy(
"UserOwnsTheOrder",
policy => policy.Requirements.Add(new UserOwnsTheOrderRequirement()));
cfg.AddPolicy(
"UserIsAdministrator",
policy => policy.RequireRole(UserAuthorizeRole.Operator));

});
services.AddTransient();
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.