mirror of https://github.com/abpframework/abp.git
committed by
GitHub
14 changed files with 141 additions and 122 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.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)))); |
||||
|
|
||||
|
await next(context); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using Volo.Abp.Security.Claims; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Security.Claims |
||||
|
{ |
||||
|
public class AbpClaimsMapOptions |
||||
|
{ |
||||
|
public Dictionary<string, string> Maps { get; } |
||||
|
|
||||
|
public AbpClaimsMapOptions() |
||||
|
{ |
||||
|
Maps = new Dictionary<string, string>() |
||||
|
{ |
||||
|
{ "sub", AbpClaimTypes.UserId }, |
||||
|
{ "role", AbpClaimTypes.Role }, |
||||
|
{ "email", AbpClaimTypes.Email }, |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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