Skip to content

Commit a70c1ff

Browse files
Adding additional claims to Identity (#17394)
* Adding additional claims to Identity * improve text * Adding review fixes * minor edits * fix build warning * fix invalid dev lang * fix another invalid dev lang Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com>
1 parent 4ad36a1 commit a70c1ff

1 file changed

Lines changed: 77 additions & 3 deletions

File tree

aspnetcore/security/authentication/add-user-data.md

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Add, download, and delete user data to Identity in an ASP.NET Core projec
33
author: rick-anderson
44
description: Learn how to add custom user data to Identity in an ASP.NET Core project. Delete data per GDPR.
55
ms.author: riande
6-
ms.date: 01/28/2020
6+
ms.date: 03/26/2020
77
ms.custom: "mvc, seodec18"
88
uid: security/authentication/add-user-data
99
---
@@ -164,7 +164,7 @@ Update the *Areas/Identity/Pages/Account/Manage/Index.cshtml* with the following
164164

165165
Update the *Areas/Identity/Pages/Account/Manage/Index.cshtml* with the following highlighted markup:
166166

167-
[!code-chtml[](add-user-data/samples/2.x/SampleApp/Areas/Identity/Pages/Account/Manage/Index.cshtml?highlight=35-42)]
167+
[!code-cshtml[](add-user-data/samples/2.x/SampleApp/Areas/Identity/Pages/Account/Manage/Index.cshtml?highlight=35-42)]
168168

169169
::: moniker-end
170170

@@ -188,7 +188,7 @@ Update the *Areas/Identity/Pages/Account/Register.cshtml* with the following hig
188188

189189
Update the *Areas/Identity/Pages/Account/Register.cshtml* with the following highlighted markup:
190190

191-
[!code-chtml[](add-user-data/samples/2.x/SampleApp/Areas/Identity/Pages/Account/Register.cshtml?highlight=16-25)]
191+
[!code-cshtml[](add-user-data/samples/2.x/SampleApp/Areas/Identity/Pages/Account/Register.cshtml?highlight=16-25)]
192192

193193
::: moniker-end
194194

@@ -222,3 +222,77 @@ Test the app:
222222
* Register a new user.
223223
* View the custom user data on the `/Identity/Account/Manage` page.
224224
* Download and view the users personal data from the `/Identity/Account/Manage/PersonalData` page.
225+
226+
## Add claims to Identity using IUserClaimsPrincipalFactory<ApplicationUser>
227+
228+
Additional claims can be added to ASP.NET Core Identity by using the `IUserClaimsPrincipalFactory<T>` interface. This class can be added to the app in the `Startup.ConfigureServices` method. Add the custom implementation of the class as follows:
229+
230+
```csharp
231+
public void ConfigureServices(IServiceCollection services)
232+
{
233+
services.AddIdentity<ApplicationUser, IdentityRole>()
234+
.AddEntityFrameworkStores<ApplicationDbContext>()
235+
.AddDefaultTokenProviders();
236+
237+
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>,
238+
AdditionalUserClaimsPrincipalFactory>();
239+
```
240+
241+
The demo code uses the `ApplicationUser` class. This class adds an `IsAdmin` property which is used to add the additional claim.
242+
243+
```csharp
244+
public class ApplicationUser : IdentityUser
245+
{
246+
public bool IsAdmin { get; set; }
247+
}
248+
```
249+
250+
The `AdditionalUserClaimsPrincipalFactory` implements the `UserClaimsPrincipalFactory` interface. A new role claim is added to the `ClaimsPrincipal`.
251+
252+
```csharp
253+
public class AdditionalUserClaimsPrincipalFactory
254+
: UserClaimsPrincipalFactory<ApplicationUser, IdentityRole>
255+
{
256+
public AdditionalUserClaimsPrincipalFactory(
257+
UserManager<ApplicationUser> userManager,
258+
RoleManager<IdentityRole> roleManager,
259+
IOptions<IdentityOptions> optionsAccessor)
260+
: base(userManager, roleManager, optionsAccessor)
261+
{}
262+
263+
public async override Task<ClaimsPrincipal> CreateAsync(ApplicationUser user)
264+
{
265+
var principal = await base.CreateAsync(user);
266+
var identity = (ClaimsIdentity)principal.Identity;
267+
268+
var claims = new List<Claim>();
269+
if (user.IsAdmin)
270+
{
271+
claims.Add(new Claim(JwtClaimTypes.Role, "admin"));
272+
}
273+
else
274+
{
275+
claims.Add(new Claim(JwtClaimTypes.Role, "user"));
276+
}
277+
278+
identity.AddClaims(claims);
279+
return principal;
280+
}
281+
}
282+
```
283+
284+
The additional claim can then be used in the app. In a Razor Page, the `IAuthorizationService` instance can be used to access the claim value.
285+
286+
```cshtml
287+
@using Microsoft.AspNetCore.Authorization
288+
@inject IAuthorizationService AuthorizationService
289+
290+
@if ((await AuthorizationService.AuthorizeAsync(User, "IsAdmin")).Succeeded)
291+
{
292+
<ul class="mr-auto navbar-nav">
293+
<li class="nav-item">
294+
<a class="nav-link" asp-controller="Admin" asp-action="Index">ADMIN</a>
295+
</li>
296+
</ul>
297+
}
298+
```

0 commit comments

Comments
 (0)