mirror of https://github.com/abpframework/abp.git
6 changed files with 126 additions and 3 deletions
@ -0,0 +1,29 @@ |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Security.Claims; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Security.Claims |
|||
{ |
|||
public class AbpClaimsMapMiddleware : IMiddleware, ITransientDependency |
|||
{ |
|||
public async Task InvokeAsync(HttpContext context, RequestDelegate next) |
|||
{ |
|||
var currentPrincipalAccessor = context.RequestServices.GetRequiredService<ICurrentPrincipalAccessor>(); |
|||
var mapOptions = context.RequestServices.GetRequiredService<IOptions<AbpClaimsMapOptions>>().Value; |
|||
|
|||
var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => mapOptions.Map.Keys.Contains(p.Type)); |
|||
currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim( |
|||
mapOptions.Map[p.Type], |
|||
p.Value, |
|||
p.ValueType, |
|||
p.Issuer)))); |
|||
|
|||
await next(context); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Security.Claims |
|||
{ |
|||
public class AbpClaimsMapOptions |
|||
{ |
|||
public Dictionary<string, string> Map { get; } |
|||
|
|||
public AbpClaimsMapOptions() |
|||
{ |
|||
Map = new Dictionary<string, string>(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using System.Security.Claims; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using Shouldly; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Security.Claims |
|||
{ |
|||
public class ClaimsMapTestController : AbpController |
|||
{ |
|||
public ActionResult ClaimsMapTest() |
|||
{ |
|||
var serialNumber = CurrentUser.FindClaim(ClaimTypes.SerialNumber); |
|||
serialNumber.ShouldNotBeNull(); |
|||
serialNumber.Value.ShouldBe("123456"); |
|||
|
|||
var dateOfBirth = CurrentUser.FindClaim(ClaimTypes.DateOfBirth); |
|||
dateOfBirth.ShouldNotBeNull(); |
|||
dateOfBirth.Value.ShouldBe("2020"); |
|||
|
|||
return Content("OK"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System.Security.Claims; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.AspNetCore.Mvc.Authorization; |
|||
using Volo.Abp.AspNetCore.TestBase; |
|||
using Volo.Abp.Autofac; |
|||
using Volo.Abp.MemoryDb; |
|||
using Volo.Abp.Modularity; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Mvc.Security.Claims |
|||
{ |
|||
[DependsOn( |
|||
typeof(AbpAspNetCoreTestBaseModule), |
|||
typeof(AbpMemoryDbTestModule), |
|||
typeof(AbpAspNetCoreMvcModule), |
|||
typeof(AbpAutofacModule) |
|||
)] |
|||
public class ClaimsMapTestController_Tests : AspNetCoreMvcTestBase |
|||
{ |
|||
private readonly FakeUserClaims _fakeRequiredService; |
|||
|
|||
public ClaimsMapTestController_Tests() |
|||
{ |
|||
_fakeRequiredService = GetRequiredService<FakeUserClaims>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Claims_Should_Be_Mapped() |
|||
{ |
|||
_fakeRequiredService.Claims.AddRange(new[] |
|||
{ |
|||
new Claim("SerialNumber", "123456"), |
|||
new Claim("DateOfBirth", "2020") |
|||
}); |
|||
|
|||
var result = await GetResponseAsStringAsync("/ClaimsMapTest/ClaimsMapTest"); |
|||
result.ShouldBe("OK"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue