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 2f2c0af116..7740ddfca2 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 @@ -16,9 +16,9 @@ namespace Volo.Abp.AspNetCore.Security.Claims var currentPrincipalAccessor = context.RequestServices.GetRequiredService(); var mapOptions = context.RequestServices.GetRequiredService>().Value; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => mapOptions.Map.Keys.Contains(p.Type)); + var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => mapOptions.Maps.Keys.Contains(p.Type)); currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim( - mapOptions.Map[p.Type], + mapOptions.Maps[p.Type], p.Value, p.ValueType, p.Issuer)))); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapOptions.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapOptions.cs index b7855df497..a5eabd76bf 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapOptions.cs @@ -1,14 +1,20 @@ using System.Collections.Generic; +using Volo.Abp.Security.Claims; namespace Volo.Abp.AspNetCore.Security.Claims { public class AbpClaimsMapOptions { - public Dictionary Map { get; } + public Dictionary Maps { get; } public AbpClaimsMapOptions() { - Map = new Dictionary(); + Maps = new Dictionary() + { + { "sub", AbpClaimTypes.UserId }, + { "role", AbpClaimTypes.Role }, + { "email", AbpClaimTypes.Email }, + }; } } } 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 ead51147c9..497dd2f45f 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 @@ -88,8 +88,8 @@ namespace Volo.Abp.AspNetCore.Mvc Configure(options => { - options.Map.Add("SerialNumber", ClaimTypes.SerialNumber); - options.Map.Add("DateOfBirth", ClaimTypes.DateOfBirth); + options.Maps.Add("SerialNumber", ClaimTypes.SerialNumber); + options.Maps.Add("DateOfBirth", ClaimTypes.DateOfBirth); }); } diff --git a/samples/MicroserviceDemo/gateways/BackendAdminAppGateway.Host/BackendAdminAppGatewayHostModule.cs b/samples/MicroserviceDemo/gateways/BackendAdminAppGateway.Host/BackendAdminAppGatewayHostModule.cs index 7edcd7f29a..11dfe16dc3 100644 --- a/samples/MicroserviceDemo/gateways/BackendAdminAppGateway.Host/BackendAdminAppGatewayHostModule.cs +++ b/samples/MicroserviceDemo/gateways/BackendAdminAppGateway.Host/BackendAdminAppGatewayHostModule.cs @@ -110,21 +110,8 @@ namespace BackendAdminAppGateway.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); + app.UseAbpClaimsMap(); - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); if (MsDemoConsts.IsMultiTenancyEnabled) { app.UseMultiTenancy(); diff --git a/samples/MicroserviceDemo/gateways/InternalGateway.Host/InternalGatewayHostModule.cs b/samples/MicroserviceDemo/gateways/InternalGateway.Host/InternalGatewayHostModule.cs index dcad85fa57..f528957fdf 100644 --- a/samples/MicroserviceDemo/gateways/InternalGateway.Host/InternalGatewayHostModule.cs +++ b/samples/MicroserviceDemo/gateways/InternalGateway.Host/InternalGatewayHostModule.cs @@ -89,21 +89,7 @@ namespace InternalGateway.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); - - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); + app.UseAbpClaimsMap(); if (MsDemoConsts.IsMultiTenancyEnabled) { app.UseMultiTenancy(); diff --git a/samples/MicroserviceDemo/gateways/PublicWebSiteGateway.Host/PublicWebSiteGatewayHostModule.cs b/samples/MicroserviceDemo/gateways/PublicWebSiteGateway.Host/PublicWebSiteGatewayHostModule.cs index 2bd570d56d..3411598ccf 100644 --- a/samples/MicroserviceDemo/gateways/PublicWebSiteGateway.Host/PublicWebSiteGatewayHostModule.cs +++ b/samples/MicroserviceDemo/gateways/PublicWebSiteGateway.Host/PublicWebSiteGatewayHostModule.cs @@ -85,21 +85,7 @@ namespace PublicWebSiteGateway.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); - - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); + app.UseAbpClaimsMap(); if (MsDemoConsts.IsMultiTenancyEnabled) { app.UseMultiTenancy(); diff --git a/samples/MicroserviceDemo/microservices/BloggingService.Host/BloggingServiceHostModule.cs b/samples/MicroserviceDemo/microservices/BloggingService.Host/BloggingServiceHostModule.cs index bc9209c72a..3542ec2d5c 100644 --- a/samples/MicroserviceDemo/microservices/BloggingService.Host/BloggingServiceHostModule.cs +++ b/samples/MicroserviceDemo/microservices/BloggingService.Host/BloggingServiceHostModule.cs @@ -121,21 +121,7 @@ namespace BloggingService.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); - - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); + app.UseAbpClaimsMap(); if (MsDemoConsts.IsMultiTenancyEnabled) { diff --git a/samples/MicroserviceDemo/microservices/IdentityService.Host/IdentityServiceHostModule.cs b/samples/MicroserviceDemo/microservices/IdentityService.Host/IdentityServiceHostModule.cs index 457950cba4..9b96532e4c 100644 --- a/samples/MicroserviceDemo/microservices/IdentityService.Host/IdentityServiceHostModule.cs +++ b/samples/MicroserviceDemo/microservices/IdentityService.Host/IdentityServiceHostModule.cs @@ -102,26 +102,13 @@ namespace IdentityService.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); + app.UseAbpClaimsMap(); if (MsDemoConsts.IsMultiTenancyEnabled) { app.UseMultiTenancy(); } - - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); + app.UseAbpRequestLocalization(); //TODO: localization? app.UseSwagger(); app.UseSwaggerUI(options => diff --git a/samples/MicroserviceDemo/microservices/ProductService.Host/ProductServiceHostModule.cs b/samples/MicroserviceDemo/microservices/ProductService.Host/ProductServiceHostModule.cs index 4968ce75a4..74a1213a83 100644 --- a/samples/MicroserviceDemo/microservices/ProductService.Host/ProductServiceHostModule.cs +++ b/samples/MicroserviceDemo/microservices/ProductService.Host/ProductServiceHostModule.cs @@ -105,21 +105,7 @@ namespace ProductService.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); - - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); + app.UseAbpClaimsMap(); if (MsDemoConsts.IsMultiTenancyEnabled) { diff --git a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs index 504713c047..8c01363e7d 100644 --- a/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs +++ b/samples/MicroserviceDemo/microservices/TenantManagementService.Host/TenantManagementServiceHostModule.cs @@ -106,21 +106,7 @@ namespace TenantManagementService.Host app.UseVirtualFiles(); app.UseRouting(); app.UseAuthentication(); - - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); + app.UseAbpClaimsMap(); if (MsDemoConsts.IsMultiTenancyEnabled) { diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs index fa3ad928f4..14aee9bbfa 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs @@ -168,25 +168,11 @@ namespace MyCompanyName.MyProjectName app.UseRouting(); app.UseCors(DefaultCorsPolicyName); app.UseAuthentication(); - + app.UseAbpClaimsMap(); if (MultiTenancyConsts.IsEnabled) { app.UseMultiTenancy(); } - app.Use(async (ctx, next) => - { - var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService(); - var map = new Dictionary() - { - { "sub", AbpClaimTypes.UserId }, - { "role", AbpClaimTypes.Role }, - { "email", AbpClaimTypes.Email }, - //any other map - }; - var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList(); - currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer)))); - await next(); - }); app.UseAbpRequestLocalization(); app.UseAuthorization(); app.UseSwagger();