From ff3f0b24366c2defd0752cd3fb28be3a99703dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 20 May 2020 19:08:18 +0300 Subject: [PATCH] Refactor AbpClaimsMapMiddleware --- .../Security/Claims/AbpClaimsMapMiddleware.cs | 33 ++++++++++++++----- .../Mvc/AbpAspNetCoreMvcTestModule.cs | 4 --- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs index 7740ddfca2..2c64234992 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs @@ -13,15 +13,32 @@ namespace Volo.Abp.AspNetCore.Security.Claims { public async Task InvokeAsync(HttpContext context, RequestDelegate next) { - var currentPrincipalAccessor = context.RequestServices.GetRequiredService(); - var mapOptions = context.RequestServices.GetRequiredService>().Value; + var currentPrincipalAccessor = context.RequestServices + .GetRequiredService(); - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => mapOptions.Maps.Keys.Contains(p.Type)); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim( - mapOptions.Maps[p.Type], - p.Value, - p.ValueType, - p.Issuer)))); + var mapOptions = context.RequestServices + .GetRequiredService>().Value; + + var mapClaims = currentPrincipalAccessor + .Principal + .Claims + .Where(claim => mapOptions.Maps.Keys.Contains(claim.Type)); + + currentPrincipalAccessor + .Principal + .AddIdentity( + new ClaimsIdentity( + mapClaims + .Select( + claim => new Claim( + mapOptions.Maps[claim.Type], + claim.Value, + claim.ValueType, + claim.Issuer + ) + ) + ) + ); await next(context); } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs index 497dd2f45f..2f9f13fdfe 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcTestModule.cs @@ -1,10 +1,7 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Security.Claims; using Localization.Resources.AbpUi; using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.AspNetCore.Mvc.Authorization; @@ -16,7 +13,6 @@ using Volo.Abp.Autofac; using Volo.Abp.Localization; using Volo.Abp.MemoryDb; using Volo.Abp.Modularity; -using Volo.Abp.Security.Claims; using Volo.Abp.TestApp; using Volo.Abp.Validation.Localization; using Volo.Abp.VirtualFileSystem;