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/authentication/claims.md
+28-25Lines changed: 28 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,36 +1,38 @@
1
1
---
2
-
title: Mapping, customizing, and transforming claims in ASP.NET Core
2
+
title: Map, customize, transform claims in ASP.NET Core
3
3
author: damienbod
4
-
description: Learn how to map claims, do claims transformations, customize claims.
4
+
description: Learn how to map claims, perform claims transformations, and customize claims in your in ASP.NET Core apps.
5
5
monikerRange: '>= aspnetcore-3.1'
6
6
ms.author: wpickett
7
7
ms.custom: mvc
8
-
ms.date: 2/16/2022
8
+
ms.date: 05/15/2026
9
9
uid: security/authentication/claims
10
+
11
+
# customer intent: As an ASP.NET developer, I want to work with claims in my ASP.NET Core app, so I can map claims, do transformations, and make customizations.
10
12
---
11
-
# Mapping, customizing, and transforming claims in ASP.NET Core
13
+
# Map, customize, and transform claims in ASP.NET Core
12
14
13
15
By [Damien Bowden](https://github.com/damienbod)
14
16
15
17
:::moniker range=">= aspnetcore-6.0"
16
18
17
-
Claims can be created from any user or identity data which can be issued using a trusted identity provider or ASP.NET Core identity. A claim is a name value pair that represents what the subject is, not what the subject can do.
18
-
This article covers the following areas:
19
+
Claims can be created from any user or identity data that can be issued by using a trusted identity provider or ASP.NET Core identity. A claim is a name-value pair that represents what the subject is, not what the subject can do.
19
20
20
-
* How to configure and map claims using an [OpenID Connect](https://openid.net/developers/how-connect-works/) client
21
-
* Set the name and role claim
21
+
This article describes how to configure and map claims by using an [OpenID Connect](https://openid.net/developers/how-connect-works/) client and covers the following tasks:
22
+
23
+
* Set name and role claims
22
24
* Reset the claims namespaces
23
-
* Customize, extend the claims using <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation.TransformAsync%2A>
25
+
* Customize and extend the claims with the <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation.TransformAsync%2A> method
24
26
25
-
## Mapping claims using OpenID Connect authentication
27
+
## Map claims with OpenID Connect authentication
26
28
27
-
The profile claims can be returned in the `id_token`, which is returned after a successful authentication. The ASP.NET Core client app only requires the profile scope. When using the `id_token` for claims, no extra claims mapping is required.
29
+
The profile claims can be returned in the `id_token`, which is returned after a successful authentication. The ASP.NET Core client app only requires the profile scope. When you use the `id_token` for claims, no extra claims mapping is required.
The preceding code requires the [Microsoft.AspNetCore.Authentication.OpenIdConnect](https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.OpenIdConnect) NuGet package.
32
34
33
-
Another way to get the user claims is to use the OpenID Connect User Info API. The ASP.NET Core client app uses the `GetClaimsFromUserInfoEndpoint` property to configure this. One important difference from the first settings, is that you must specify the claims you require using the `MapUniqueJsonKey` method, otherwise only the `name`, `given_name` and `email` standard claims will be available in the client app. The claims included in the `id_token` are mapped per default. This is the major difference to the first option. You must explicitly define some of the claims you require.
35
+
Another way to get the user claims is to use the OpenID Connect User Info API. The ASP.NET Core client app uses the `GetClaimsFromUserInfoEndpoint` property to do the configuration. In this scenario, you must explicitly specify the required claims by using the `MapUniqueJsonKey` method. Otherwise, only the `name`, `given_name`, and `email` standard claims are available in the client app. The claims included in the `id_token` are mapped per default.
@@ -39,27 +41,27 @@ Another way to get the user claims is to use the OpenID Connect User Info API. T
39
41
:::moniker range="> aspnetcore-8.0"
40
42
41
43
> [!NOTE]
42
-
> The default Open ID Connect handler uses Pushed Authorization Requests (PAR) if the identity provider's discovery document advertises support for PAR. The identity provider's discovery document is usually found at `.well-known/openid-configuration`. If you cannot use PAR in the client configuration on the identity provider, PAR can be disabled by using the **PushedAuthorizationBehavior** option.
44
+
> The default Open ID Connect handler uses Pushed Authorization Requests (PAR) if the identity provider's discovery document advertises support for PAR. The common location for the identity provider's discovery document is the _.well-known/openid-configuration_ folder. If you can't use PAR in the client configuration on the identity provider, PAR can be disabled by using the `PushedAuthorizationBehavior` option.
To ensure that authentication only succeeds if PAR is used, use [`PushedAuthorizationBehavior.Require`](xref:Microsoft.AspNetCore.Authentication.OpenIdConnect.PushedAuthorizationBehavior) instead. This change also introduces a new <xref:Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents.OnPushAuthorization> event to <xref:Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents> which can be used to customize the pushed authorization request or handle it manually. For more information, see the [API proposal](https://github.com/dotnet/aspnetcore/issues/51686).
48
+
To ensure authentication succeeds only if PAR is used, use the [PushedAuthorizationBehavior.Require](xref:Microsoft.AspNetCore.Authentication.OpenIdConnect.PushedAuthorizationBehavior)enum instead. This change also introduces a new <xref:Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents.OnPushAuthorization> event to <xref:Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectEvents>, which can be used to customize the pushed authorization request or handle it manually. For more information, see the [API proposal in GitHub dotnet/aspnetcore issue #51686](https://github.com/dotnet/aspnetcore/issues/51686) - _Support for Pushed Authorization Requests in OidcHandler_.
47
49
48
50
:::moniker-end
49
51
50
52
:::moniker range=">= aspnetcore-6.0"
51
53
52
54
## Name claim and role claim mapping
53
55
54
-
The **Name** claim and the **Role** claim are mapped to default properties in the ASP.NET Core HTTP context. Sometimes it is required to use different claims for the default properties, or the name claim and the role claim do not match the default values. The claims can be mapped using the **TokenValidationParameters** property and set to any claim as required. The values from the claims can be used directly in the HttpContext **User.Identity.Name** property and the roles.
56
+
The **name** claim and **role** claim are mapped to default properties in the ASP.NET Core HTTP context. Sometimes, you need to use different claims for the default properties, or the name claim and role claim don't match the default values. The claims can be mapped by using the `TokenValidationParameters` property and set to any claim as required. The values from the claims can be used directly in the HttpContext `User.Identity.Name` property and the roles.
55
57
56
-
If the `User.Identity.Name` has no value or the roles are missing, please check the values in the returned claims and set the `NameClaimType` and the `RoleClaimType` values. The returned claims from the client authentication can be viewed in the HTTP context.
58
+
If the `User.Identity.Name`property has no value, or the roles are missing, check the values in the returned claims and set the `NameClaimType` and the `RoleClaimType` values. The returned claims from the client authentication can be viewed in the HTTP context.
ASP.NET Core adds default namespaces to some known claims, which might not be required in the app. Optionally, disable these added namespaces and use the exact claims that the OpenID Connect server created.
64
+
ASP.NET Core adds default namespaces to some known claims. The default claims might not be required in the app. As an option, you can disable the added namespaces and use the exact claims that the OpenID Connect server created.
63
65
64
66
:::moniker-end
65
67
@@ -68,7 +70,6 @@ ASP.NET Core adds default namespaces to some known claims, which might not be re
## Extend or add custom claims using `IClaimsTransformation`
85
+
## Extend or add custom claims using 'IClaimsTransformation'
85
86
86
-
The <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation> interface can be used to add extra claims to the <xref:System.Security.Claims.ClaimsPrincipal> class. The interface requires a single method <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation.TransformAsync%2A>. This method might get called multiple times. Only add a new claim if it does not already exist in the `ClaimsPrincipal`. A `ClaimsIdentity` is created to add the new claims and this can be added to the `ClaimsPrincipal`.
87
+
The <xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation> interface can be used to add extra claims to the <xref:System.Security.Claims.ClaimsPrincipal> class. The interface requires a single method,<xref:Microsoft.AspNetCore.Authentication.IClaimsTransformation.TransformAsync%2A>. This method might be called multiple times. Only add a new claim if it doesn't already exist in the `ClaimsPrincipal`. A `ClaimsIdentity`object is created to add the new claims and it can be added to the `ClaimsPrincipal`.
@@ -103,13 +104,10 @@ Refer to the following document:
103
104
104
105
## Map claims from external identity providers
105
106
106
-
Refer to the following document:
107
-
108
-
[Persist additional claims and tokens from external providers in ASP.NET Core](xref:security/authentication/social/additional-claims)
107
+
If you want to map claims from external identity providers for your app, see [Persist other claims and tokens from external providers in ASP.NET Core](xref:security/authentication/social/additional-claims).
109
108
110
109
:::moniker-end
111
110
112
-
113
111
:::moniker range="< aspnetcore-6.0"
114
112
115
113
Claims can be created from any user or identity data which can be issued using a trusted identity provider or ASP.NET Core identity. A claim is a name value pair that represents what the subject is, not what the subject can do.
@@ -254,3 +252,8 @@ Refer to the following document:
0 commit comments