-
Notifications
You must be signed in to change notification settings - Fork 168
Add custom policy provider example to authz BWA #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
guardrex
merged 18 commits into
main
from
guardrex/add-iauthorizationpolicyprovider-to-bwa
Jul 29, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e62cff5
Add custom policy provider example to authz BWA
guardrex 642f0fe
Apply suggestions from code review
guardrex 2820e53
Updates
guardrex 45d38d4
Updates
guardrex fd52a12
Updates
guardrex f4d3cd1
Updates
guardrex 741666b
Updates
guardrex 43ea78d
Updates
guardrex 7594130
Updates
guardrex 693a794
Apply suggestions from code review
guardrex ae185a1
Clarify policy satisfaction language in README
guardrex f4140da
Clarify DateOfBirth claim descriptions in README
guardrex 7d662ee
Updates
guardrex 5a259d5
Updates
guardrex 0bedccf
Updates
guardrex 7a5547d
Update security/authorization/BlazorWebAppAuthorization/Components/Pa…
guardrex bfb9cb0
Update caps in link text in the Home component
guardrex 80ebed7
Merge branch 'guardrex/add-iauthorizationpolicyprovider-to-bwa' of ht…
guardrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
18 changes: 18 additions & 0 deletions
18
...ity/authorization/BlazorWebAppAuthorization/Components/Pages/PassMinimumAge21Policy.razor
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| @page "/pass-minimumage21-policy" | ||
|
|
||
| <h1>Pass 'MinimumAge21' policy (weakly-typed approach)</h1> | ||
|
|
||
| <p> | ||
| Uses an AuthorizeView component to apply the policy using the policy's name. | ||
| This approach is shown for demonstration purposes and isn't recommended for | ||
| production code. | ||
| </p> | ||
|
|
||
| <AuthorizeView Policy="MinimumAge21"> | ||
| <Authorized> | ||
| <p>You satisfy the 'MinimumAge21' policy.</p> | ||
| </Authorized> | ||
| <NotAuthorized> | ||
| <p>You <b>don't</b> satisfy the 'MinimumAge21' policy.</p> | ||
| </NotAuthorized> | ||
| </AuthorizeView> |
14 changes: 14 additions & 0 deletions
14
...tion/BlazorWebAppAuthorization/Components/Pages/PassMinimumAge21PolicyWithAttribute.razor
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| @page "/pass-minimumage21-policy-with-attribute" | ||
| @using BlazorWebAppAuthorization.Policies.Attributes | ||
| @attribute [MinimumAgeAuthorize(21)] | ||
|
|
||
| <h1>Pass 'MinimumAge21' policy (strongly-typed approach)</h1> | ||
|
|
||
| <p> | ||
| Applies the policy to the Razor component with a custom | ||
| [MinimumAgeAuthorize] attribute (derived from AuthorizeAttribute). | ||
| This approach is preferred for production code, as it's strongly-typed | ||
| and avoids the use of a string to set the policy and minimum age. | ||
| </p> | ||
|
|
||
| <p>You satisfy the 'MinimumAge21' policy.</p> |
31 changes: 31 additions & 0 deletions
31
...thorization/BlazorWebAppAuthorization/Policies/Attributes/MinimumAgeAuthorizeAttribute.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| using Microsoft.AspNetCore.Authorization; | ||
|
|
||
| namespace BlazorWebAppAuthorization.Policies.Attributes; | ||
|
|
||
| public class MinimumAgeAuthorizeAttribute : AuthorizeAttribute | ||
| { | ||
| private const string PolicyPrefix = "MinimumAge"; | ||
|
|
||
| public MinimumAgeAuthorizeAttribute(int age) => Age = age; | ||
|
|
||
| public int Age | ||
| { | ||
| get | ||
| { | ||
| if (!string.IsNullOrEmpty(Policy) && | ||
| Policy.StartsWith(PolicyPrefix, | ||
| StringComparison.OrdinalIgnoreCase) && | ||
| int.TryParse(Policy.AsSpan(PolicyPrefix.Length), out var age)) | ||
| { | ||
| return age; | ||
| } | ||
|
|
||
| return default; | ||
| } | ||
| set | ||
| { | ||
| ArgumentOutOfRangeException.ThrowIfNegative(value); | ||
| Policy = $"{PolicyPrefix}{value}"; | ||
| } | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
...ty/authorization/BlazorWebAppAuthorization/Policies/Providers/MinimumAgePolicyProvider.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| using BlazorWebAppAuthorization.Policies.Requirements; | ||
| using Microsoft.AspNetCore.Authorization; | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.Extensions.Options; | ||
|
|
||
| namespace BlazorWebAppAuthorization.Policies.Providers; | ||
|
|
||
| public class MinimumAgePolicyProvider(IOptions<AuthorizationOptions> options) | ||
| : IAuthorizationPolicyProvider | ||
| { | ||
| private const string PolicyPrefix = "MinimumAge"; | ||
|
|
||
| private DefaultAuthorizationPolicyProvider DefaultPolicyProvider { get; } = | ||
| new(options); | ||
|
|
||
| public Task<AuthorizationPolicy?> GetPolicyAsync(string policyName) | ||
| { | ||
| if (policyName.StartsWith( | ||
| PolicyPrefix, StringComparison.OrdinalIgnoreCase) && | ||
| int.TryParse(policyName.AsSpan(PolicyPrefix.Length), out var age) && | ||
| age >= 0) | ||
| { | ||
| var policy = new AuthorizationPolicyBuilder( | ||
| IdentityConstants.ApplicationScheme); | ||
| policy.AddRequirements(new MinimumAgeRequirement(age)); | ||
|
|
||
| return Task.FromResult<AuthorizationPolicy?>(policy.Build()); | ||
| } | ||
|
|
||
| return DefaultPolicyProvider.GetPolicyAsync(policyName); | ||
| } | ||
|
|
||
| public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => | ||
| DefaultPolicyProvider.GetDefaultPolicyAsync(); | ||
|
|
||
| public Task<AuthorizationPolicy?> GetFallbackPolicyAsync() => | ||
| DefaultPolicyProvider.GetFallbackPolicyAsync(); | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.