Add custom policy provider example to authz BWA#318
Open
guardrex wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
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
AuthorizeViewand an attribute-based approach. - Update the README’s seeded-user notes to include the new
MinimumAge21policy 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.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.