Skip to content

Add custom policy provider example to authz BWA#318

Open
guardrex wants to merge 3 commits into
mainfrom
guardrex/add-iauthorizationpolicyprovider-to-bwa
Open

Add custom policy provider example to authz BWA#318
guardrex wants to merge 3 commits into
mainfrom
guardrex/add-iauthorizationpolicyprovider-to-bwa

Conversation

@guardrex

@guardrex guardrex commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

Addresses dotnet/AspNetCore.Docs#37370

Wade, these are the BWA authz sample updates for the custom authz policy provider doc overhaul, which is a WIP at dotnet/AspNetCore.Docs#37379. We can go ahead and merge these, as I don't think that they'll change ... well 😄 ... unless Stephen says that my 🙈 RexHacks!™ 🙈 need some touches. If so tho, I'll set them up on another PR. I'd rather put these into the sample app so that he can pull it down and run it with the updates if he wants to, so I'm going ahead and requesting your review now. I'll wrap up the article work and ping him for review over there on Monday.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a custom IAuthorizationPolicyProvider example to the Blazor Web App authorization sample to demonstrate dynamically generated “MinimumAge*” policies (as requested in dotnet/AspNetCore.Docs#37370).

Changes:

  • Register a custom authorization policy provider and add dynamic MinimumAge{N} policy support.
  • Add two new pages demonstrating the dynamic policy via AuthorizeView and an attribute-based approach.
  • Update the README’s seeded-user notes to include the new MinimumAge21 policy behavior.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
security/authorization/BlazorWebAppAuthorization/README.md Updates seeded account descriptions to reference MinimumAge21 in addition to AtLeast21.
security/authorization/BlazorWebAppAuthorization/Program.cs Registers the custom IAuthorizationPolicyProvider.
security/authorization/BlazorWebAppAuthorization/Policies/Providers/MinimumAgePolicyProvider.cs Adds a dynamic policy provider for MinimumAge{N} policies.
security/authorization/BlazorWebAppAuthorization/Policies/Attributes/MinimumAgeAuthorizeAttribute.cs Adds a helper attribute for specifying MinimumAge{N} policies declaratively.
security/authorization/BlazorWebAppAuthorization/Components/Pages/PassMinimumAge21PolicyWithAttribute.razor New page showing attribute-based authorization for MinimumAge21.
security/authorization/BlazorWebAppAuthorization/Components/Pages/PassMinimumAge21Policy.razor New page showing AuthorizeView policy-based authorization for MinimumAge21.
security/authorization/BlazorWebAppAuthorization/Components/Pages/Home.razor Adds navigation links to the new policy provider example pages.
Comments suppressed due to low confidence (3)

security/authorization/BlazorWebAppAuthorization/Policies/Providers/MinimumAgePolicyProvider.cs:23

  • Returning null for unknown policies prevents policies defined via AddAuthorizationBuilder (for example, "AtLeast21") from being found once this provider is registered. Delegate to the fallback policy provider for non-MinimumAge* policies.
        return Task.FromResult<AuthorizationPolicy?>(null);

security/authorization/BlazorWebAppAuthorization/Policies/Providers/MinimumAgePolicyProvider.cs:30

  • GetDefaultPolicyAsync should delegate to the fallback policy provider so the app’s configured DefaultPolicy (and any future changes to it) are honored, rather than hardcoding a new policy here.
    public Task<AuthorizationPolicy> GetDefaultPolicyAsync() =>
        Task.FromResult<AuthorizationPolicy>(
            new AuthorizationPolicyBuilder(
                IdentityConstants.ApplicationScheme)
            .RequireAuthenticatedUser().Build());

security/authorization/BlazorWebAppAuthorization/Policies/Providers/MinimumAgePolicyProvider.cs:33

  • GetFallbackPolicyAsync should delegate to the fallback policy provider so any configured FallbackPolicy is honored (instead of always returning null).
    public Task<AuthorizationPolicy?> GetFallbackPolicyAsync() =>
        Task.FromResult<AuthorizationPolicy?>(null);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

guardrex and others added 2 commits July 24, 2026 13:11
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@guardrex
guardrex requested a review from wadepickett July 24, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants