Skip to content

Add AuthorizationPolicyCache #43227

Closed
@halter73

Description

@halter73

Background and Motivation

From @HaoK: We want to add a new metadata class which if applied to an endpoint, the authorization middleware will store the combined policy in this metadata to avoid recomputing the policy on every request (but only if the default policy provider is being used). The benefit for this being public is so anyone can benefit from this behavior by adding it to an endpoints metadata.

Proposed API

namespace Microsoft.AspNetCore.Authorization;

public interface IAuthorizationPolicyProvider
{
+    public bool CanCachePolicies => false;
}

public class AuthorizationMiddleware
{
+    public AuthorizationMiddleware(RequestDelegate next, IAuthorizationPolicyProvider policyProvider, IServiceProvider services);
}

Usage Examples

public class MyAuthorizationPolicyProvider : IAuthorizationPolicyProvider
{
    public bool CanCachePolicies => true;
}

Alternative Designs

Keep this internal for now and only use it from AuthorizationMiddleware.

Risks

We might want to change this class in the future and will be unable to make breaking changes since the metadata is now public.


PR for context: #43124
This is a follow up to the previously-approved RequireAuthorization overloads supporting inline policies: #39840

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-authIncludes: Authn, Authz, OAuth, OIDC, Bearer

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions