Course contentsShow
← C#
Lesson 1214 of 1,90811. Identity, Security, and ClaimsFree lesson

Authorization Policy Evaluation Caching

Optimize authorization performance by caching the result of complex policy evaluations.

In this lesson, you will learn how to boost your application's responsiveness by caching the outcomes of expensive authorization policy evaluations.

The Performance Bottleneck

When you implement complex policy-based authorization—such as checking a database, querying an external API, or performing intricate logic across multiple claims—the evaluation process can become a performance bottleneck. If every request triggers a round-trip to a database to verify if a user has "Edit" permissions for a specific resource, your application will struggle under load.

Authorization policy evaluation caching solves this by storing the result of an authorization decision for a specific user and resource combination, allowing subsequent checks to bypass the heavy logic.

Implementing the Cache

Instead of re-executing your custom IAuthorizationRequirement handlers, you can implement a caching layer within your AuthorizationHandler. By utilizing IMemoryCache, you can store the boolean result of a policy evaluation keyed by a unique identifier, such as user_id + resource_id.

public class PermissionHandler : AuthorizationHandler<PermissionRequirement>
{
    private readonly IMemoryCache _cache;
    private readonly IPermissionService _db;

    protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
    {
        string userId = context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        string cacheKey = $"auth_{userId}_{requirement.PermissionName}";

        if (!_cache.TryGetValue(cacheKey, out bool isAuthorized))
        {
            // Expensive lookup happens only once
            isAuthorized = await _db.CheckPermissionAsync(userId, requirement.PermissionName);
            _cache.Set(cacheKey, isAuthorized, TimeSpan.FromMinutes(5));
        }

        if (isAuthorized)
        {
            context.Succeed(requirement);
        }
    }
}

Strategic Considerations

When caching authorization results, you must be careful about "cache staleness." If a user's role or permissions change, the cache may still hold an "authorized" flag, granting access they should no longer possess. To mitigate this, ensure your cache entries have a short Time-To-Live (TTL) or implement a cache-invalidation strategy that triggers whenever user claims or permissions are updated in your identity store.

Key Takeaway: By caching the results of expensive authorization requirements using a unique key, you significantly reduce database overhead while maintaining granular control over your security policies.