Advertisement
Intermediate ASP.NET Core, Authorization

How do you combine multiple authorization requirements within a single policy, and how are they evaluated (AND vs OR logic)?

Short answer

By default, all requirements added to a single AuthorizationPolicy must succeed (AND logic) for the policy to pass -- calling RequireClaim, RequireRole, and RequireAssertion multiple times on the same policy builder adds multiple requirements that ALL must be satisfied…

Save ASP.NET Core mock interview
Advertisement
AModel answer
By default, all requirements added to a single AuthorizationPolicy must succeed (AND logic) for the policy to pass -- calling RequireClaim, RequireRole, and RequireAssertion multiple times on the same policy builder adds multiple requirements that ALL must be satisfied; achieving OR logic between alternatives requires either a single RequireAssertion with custom OR logic inside it, or defining separate policies checked independently.
builder.Services.AddAuthorization(options => {
    options.AddPolicy("SeniorManager", policy => policy
        .RequireRole("Manager")           // AND
        .RequireClaim("years", "5", "10", "15", "20"));  // must ALSO have 5+ years

    // OR logic requires a single assertion instead:
    options.AddPolicy("ManagerOrAdmin", policy => policy
        .RequireAssertion(ctx => ctx.User.IsInRole("Manager") || ctx.User.IsInRole("Admin")));
});
Real-world example
A policy requiring both a Manager role AND a minimum tenure claim correctly rejects a newly-promoted manager who hasn't yet met the tenure requirement, since both requirements must independently succeed under the default AND evaluation.
Likely follow-ups
  • How would you debug why a policy with multiple requirements unexpectedly fails?
  • What's a cleaner way to express complex OR logic than a single large RequireAssertion?
Related topics