From a75b26cc30fd4b7da385121e31994efb388bdf76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 7 Jan 2021 15:08:05 +0300 Subject: [PATCH] Add more claims to the access token. --- .../Abp/IdentityServer/AbpClaimsService.cs | 28 +++++++++++++++---- .../AbpResourceOwnerPasswordValidator.cs | 7 ++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpClaimsService.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpClaimsService.cs index c60c241209..3e6a7c76b2 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpClaimsService.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpClaimsService.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Security.Claims; +using IdentityModel; using IdentityServer4.Services; using Microsoft.Extensions.Logging; using Volo.Abp.Security.Claims; @@ -9,6 +10,16 @@ namespace Volo.Abp.IdentityServer { public class AbpClaimsService : DefaultClaimsService { + private static readonly string[] AdditionalOptionalClaimNames = + { + AbpClaimTypes.TenantId, + AbpClaimTypes.Name, + AbpClaimTypes.SurName, + JwtClaimTypes.PreferredUserName, + JwtClaimTypes.GivenName, + JwtClaimTypes.FamilyName, + }; + public AbpClaimsService(IProfileService profile, ILogger logger) : base(profile, logger) { @@ -16,13 +27,20 @@ namespace Volo.Abp.IdentityServer protected override IEnumerable GetOptionalClaims(ClaimsPrincipal subject) { - var tenantClaim = subject.FindFirst(AbpClaimTypes.TenantId); - if (tenantClaim == null) + return base.GetOptionalClaims(subject) + .Union(GetAdditionalOptionalClaims(subject)); + } + + protected virtual IEnumerable GetAdditionalOptionalClaims(ClaimsPrincipal subject) + { + foreach (var claimName in AdditionalOptionalClaimNames) { - return base.GetOptionalClaims(subject); + var claim = subject.FindFirst(claimName); + if (claim != null) + { + yield return claim; + } } - - return base.GetOptionalClaims(subject).Union(new[] { tenantClaim }); } } } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs index eee4c001ed..e024976548 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AspNetIdentity/AbpResourceOwnerPasswordValidator.cs @@ -200,7 +200,12 @@ namespace Volo.Abp.IdentityServer.AspNetIdentity { if (user.TenantId.HasValue) { - customClaims.Add(new Claim(AbpClaimTypes.TenantId, user.TenantId?.ToString())); + customClaims.Add( + new Claim( + AbpClaimTypes.TenantId, + user.TenantId?.ToString() + ) + ); } return Task.CompletedTask;