-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Custom auth policies with IARD overhaul #36866
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 6 commits into
main
from
guardrex/custom-auth-policies-IAuthorizationRequirementData
Mar 16, 2026
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
121 changes: 121 additions & 0 deletions
121
...tion/custom-authorization-policies-with-iauthorizationrequirementdata-in-mvc.md
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,121 @@ | ||
| --- | ||
| title: Custom authorization policies with `IAuthorizationRequirementData` in ASP.NET Core MVC | ||
| ai-usage: ai-assisted | ||
| author: tdykstra | ||
| description: Learn how to specify requirements associated with the authorization policy in attribute definitions with the IAuthorizationRequirementData interface in ASP.NET Core MVC. | ||
| monikerRange: '>= aspnetcore-8.0' | ||
| ms.author: tdykstra | ||
| ms.date: 03/11/2026 | ||
| uid: mvc/security/authorization/iard | ||
| --- | ||
| # Custom authorization policies with `IAuthorizationRequirementData` in ASP.NET Core MVC | ||
|
|
||
| This article provides a demonstration on how to use <xref:Microsoft.AspNetCore.Authorization.IAuthorizationRequirementData> to define custom authorization policies in ASP.NET Core MVC. For general guidance on this subject, see <xref:security/authorization/iard>. | ||
|
|
||
| ## Sample app | ||
|
|
||
| The MVC sample for this article is the [`AuthRequirementsData` sample app (`dotnet/AspNetCore.Docs.Samples` GitHub repository)](https://github.com/dotnet/AspNetCore.Docs.Samples/tree/main/security/authorization/AuthRequirementsData) ([how to download](xref:index#how-to-download-a-sample)). The sample app implements a minimum age handler for users, requiring a user to present a birth date claim indicating that they're at least 21 years old. | ||
|
|
||
| ## Demonstration | ||
|
|
||
| Test the sample with [`dotnet user-jwts`](xref:security/authentication/jwt) and curl. | ||
|
|
||
| From the project's folder in a command shell, execute the following command to create a JWT bearer token with a birth date claim that makes the user over 21 years old: | ||
|
|
||
| ```dotnetcli | ||
| dotnet user-jwts create --claim http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=1989-01-01 | ||
| ``` | ||
|
|
||
| The output produces a token after "`Token:`" in the command shell: | ||
|
|
||
| ```dotnetcli | ||
| New JWT saved with ID '{JWT ID}'. | ||
| Name: {USER} | ||
| Custom Claims: [http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=1989-01-01] | ||
|
|
||
| Token: {TOKEN} | ||
| ``` | ||
|
|
||
| Set the value of the token (where the `{TOKEN}` placeholder appears in the preceding output) aside for use later. | ||
|
|
||
| You can decode the token in an online JWT decoder, such as [`jwt.ms`](https://jwt.ms/) to see its contents, revealing that it contains a `birthdate` claim with the user's birth date: | ||
|
|
||
| ```json | ||
| { | ||
| "alg": "HS256", | ||
| "typ": "JWT" | ||
| }.{ | ||
| "unique_name": "guard", | ||
| "sub": "guard", | ||
| "jti": "5316e1b4", | ||
| "birthdate": "1989-01-01", | ||
| "aud": "https://localhost:51100", | ||
| "nbf": 1773320013, | ||
| "exp": 1781268813, | ||
| "iat": 1773320014, | ||
| "iss": "dotnet-user-jwts" | ||
| }.[Signature] | ||
| ``` | ||
|
|
||
| Execute the command again with a `dateofbirth` value that makes the user under the age of 21: | ||
|
|
||
| ```dotnetcli | ||
| dotnet user-jwts create --claim http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=2020-01-01 | ||
| ``` | ||
|
|
||
| Set the value of second token aside. | ||
guardrex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Start the app in Visual Studio or with the `dotnet watch` command in the .NET CLI. | ||
|
|
||
| In a command shell, use the .NET CLI to execute the following `curl.exe` command to request the `api/greetings/hello` endpoint. Replace the `{TOKEN}` placeholder with the first JWT bearer token that you saved earlier: | ||
|
|
||
| ```dotnetcli | ||
| curl.exe -i -H "Authorization: Bearer {TOKEN}" https://localhost:51100/api/greetings/hello | ||
| ``` | ||
|
|
||
| The output indicates success because the user's birth date claim indicates that they're at least 21 years old: | ||
|
|
||
| ```dotnetcli | ||
| HTTP/1.1 200 OK | ||
| Content-Type: text/plain; charset=utf-8 | ||
| Date: Thu, 15 May 2025 22:58:10 GMT | ||
| Server: Kestrel | ||
| Transfer-Encoding: chunked | ||
|
|
||
| Hello {USER}! | ||
| ``` | ||
|
|
||
| Logging indicates that the age requirement was met: | ||
|
|
||
| <!-- DOC AUTHOR NOTE | ||
|
|
||
| The following block quote uses two spaces at the ends of lines (except the | ||
| last line) to create returns in the rendered content. Don't remove the two | ||
| spaces at the ends of the lines when editing the following content. | ||
|
|
||
| --> | ||
|
|
||
| > :::no-loc text="MinimumAgeAuthorizationHandler: Information: Evaluating authorization requirement for age >= 21"::: | ||
| > :::no-loc text="MinimumAgeAuthorizationHandler: Information: Minimum age authorization requirement 21 satisfied"::: | ||
|
|
||
| Re-execute the `curl.exe` command with the second token, which indicates the user is under 21 years old. The output indicates that the requirement isn't met. Access to the endpoint is forbidden (status code 403): | ||
|
|
||
| ```dotnetcli | ||
| HTTP/1.1 403 Forbidden | ||
| Content-Length: 0 | ||
| Date: Thu, 15 May 2025 22:58:36 GMT | ||
| Server: Kestrel | ||
| ``` | ||
|
|
||
| Logging indicates that the age requirement wasn't met: | ||
|
|
||
| <!-- DOC AUTHOR NOTE | ||
|
|
||
| The following block quote uses two spaces at the ends of lines (except the | ||
| last line) to create returns in the rendered content. Don't remove the two | ||
| spaces at the ends of the lines when editing the following content. | ||
|
|
||
| --> | ||
|
|
||
| > :::no-loc text="MinimumAgeAuthorizationHandler: Information: Evaluating authorization requirement for age >= 21"::: | ||
| > :::no-loc text="MinimumAgeAuthorizationHandler: Information: Current user's DateOfBirth claim (2020-01-01) doesn't satisfy the minimum age authorization requirement 21"::: | ||
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
Oops, something went wrong.
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.