You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/security/authorization/claims.md
+73-2Lines changed: 73 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,85 @@ author: wadepickett
4
4
description: Learn how to add claims checks for authorization in an ASP.NET Core app.
5
5
ms.author: wpickett
6
6
monikerRange: '>= aspnetcore-3.1'
7
-
ms.date: 11/26/2021
7
+
ms.date: 01/22/2026
8
8
uid: security/authorization/claims
9
+
ai-usage: ai-assisted
9
10
---
10
11
# Claims-based authorization in ASP.NET Core
11
12
12
13
<aname="security-authorization-claims-based"></a>
13
14
14
-
:::moniker range=">= aspnetcore-6.0"
15
+
:::moniker range=">= aspnetcore-7.0"
16
+
17
+
When an identity is created it may be assigned one or more claims issued by a trusted party. A claim is a name value pair that represents what the subject is, not what the subject can do. For example, you may have a driver's license, issued by a local driving license authority. Your driver's license has your date of birth on it. In this case the claim name would be `DateOfBirth`, the claim value would be your date of birth, for example `8th June 1970` and the issuer would be the driving license authority. Claims-based authorization, at its simplest, checks the value of a claim and allows access to a resource based upon that value. For example if you want access to a night club the authorization process might be:
18
+
19
+
The door security officer would evaluate the value of your date of birth claim and whether they trust the issuer (the driving license authority) before granting you access.
20
+
21
+
An identity can contain multiple claims with multiple values and can contain multiple claims of the same type.
22
+
23
+
## Adding claims checks
24
+
25
+
Claim-based authorization checks:
26
+
27
+
* Are declarative.
28
+
* Are applied to Razor Pages, controllers, or actions within a controller.
29
+
* Can ***not*** be applied at the Razor Page handler level, they must be applied to the Page.
30
+
31
+
Claims in code specify claims which the current user must possess, and optionally the value the claim must hold to access the requested resource. Claims requirements are policy based; the developer must build and register a policy expressing the claims requirements.
32
+
33
+
The simplest type of claim policy looks for the presence of a claim and doesn't check the value.
34
+
35
+
Build and register the policy and call <xref:Microsoft.AspNetCore.Builder.AuthorizationAppBuilderExtensions.UseAuthorization%2A>. Registering the policy takes place as part of the Authorization service configuration, typically in the `Program.cs` file:
In this case the `EmployeeOnly` policy checks for the presence of an `EmployeeNumber` claim on the current identity.
40
+
41
+
Apply the policy using the `Policy` property on the [`[Authorize]`](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute) attribute to specify the policy name.
The `[Authorize]` attribute can be applied to an entire controller or Razor Page, in which case only identities matching the policy are allowed access to any Action on the controller.
Policies can ***not*** be applied at the Razor Page handler level, they must be applied to the Page.
54
+
55
+
If you have a controller that's protected by the `[Authorize]` attribute but want to allow anonymous access to particular actions, you apply the `AllowAnonymousAttribute` attribute.
Because policies can ***not*** be applied at the Razor Page handler level, we recommend using a controller when policies must be applied at the page handler level. The rest of the app that doesn't require policies at the Razor Page handler level can use Razor Pages.
60
+
61
+
Most claims come with a value. You can specify a list of allowed values when creating the policy. The following example would only succeed for employees whose employee number was 1, 2, 3, 4, or 5.
If the claim value isn't a single value or a transformation is required, use <xref:Microsoft.AspNetCore.Authorization.AuthorizationPolicyBuilder.RequireAssertion%2A>. For more information, see [Use a func to fulfill a policy](xref:security/authorization/policies#use-a-func-to-fulfill-a-policy).
68
+
69
+
## Multiple Policy Evaluation
70
+
71
+
If multiple policies are applied at the controller and action levels, ***all*** policies must pass before access is granted:
In the preceding example any identity which fulfills the `EmployeeOnly` policy can access the `Payslip` action as that policy is enforced on the controller. However, in order to call the `UpdateSalary` action the identity must fulfill *both* the `EmployeeOnly` policy and the `HumanResources` policy.
76
+
77
+
If you want more complicated policies, such as taking a date of birth claim, calculating an age from it, and then checking that the age is 21 or older, then you need to write [custom policy handlers](xref:security/authorization/policies).
78
+
79
+
In the following sample, both page handler methods must fulfill *both* the `EmployeeOnly` policy and the `HumanResources` policy:
When an identity is created it may be assigned one or more claims issued by a trusted party. A claim is a name value pair that represents what the subject is, not what the subject can do. For example, you may have a driver's license, issued by a local driving license authority. Your driver's license has your date of birth on it. In this case the claim name would be `DateOfBirth`, the claim value would be your date of birth, for example `8th June 1970` and the issuer would be the driving license authority. Claims-based authorization, at its simplest, checks the value of a claim and allows access to a resource based upon that value. For example if you want access to a night club the authorization process might be:
Swapping to the <strong>Development</strong> environment displays detailed information about the error that occurred.
20
+
</p>
21
+
<p>
22
+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
23
+
It can result in displaying sensitive information from exceptions to end users.
24
+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
0 commit comments