mirror of https://github.com/abpframework/abp.git
5 changed files with 127 additions and 2 deletions
@ -0,0 +1,40 @@ |
|||
using System.Security.Claims; |
|||
using Microsoft.AspNetCore.Authentication.OAuth.Claims; |
|||
using Newtonsoft.Json.Linq; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Authentication.OAuth.Claims |
|||
{ |
|||
public class MultipleClaimAction : ClaimAction |
|||
{ |
|||
public MultipleClaimAction(string claimType, string jsonKey) |
|||
: base(claimType, jsonKey) |
|||
{ |
|||
} |
|||
|
|||
public override void Run(JObject userData, ClaimsIdentity identity, string issuer) |
|||
{ |
|||
var prop = userData?.Property(ValueType); |
|||
if (prop == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var propValue = prop.Value; |
|||
|
|||
switch (propValue.Type) |
|||
{ |
|||
case JTokenType.String: |
|||
identity.AddClaim(new Claim(ClaimType, propValue.Value<string>(), ValueType, issuer)); |
|||
break; |
|||
case JTokenType.Array: |
|||
foreach (var innterValue in propValue.Values<string>()) |
|||
{ |
|||
identity.AddClaim(new Claim(ClaimType, innterValue, ValueType, issuer)); |
|||
} |
|||
break; |
|||
default: |
|||
throw new AbpException("Unhandled JTokenType: " + propValue.Type); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp2.0</TargetFramework> |
|||
<LangVersion>latest</LangVersion> |
|||
<AssemblyName>Volo.Abp.AspNetCore.Authentication.OAuth.Tests</AssemblyName> |
|||
<PackageId>Volo.Abp.AspNetCore.Authentication.OAuth.Tests</PackageId> |
|||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
|||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
|||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
|||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
|||
<RootNamespace /> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
|||
<ProjectReference Include="..\..\src\Volo.Abp.AspNetCore.Authentication.OAuth\Volo.Abp.AspNetCore.Authentication.OAuth.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,46 @@ |
|||
using System.Linq; |
|||
using System.Security.Claims; |
|||
using Newtonsoft.Json.Linq; |
|||
using Shouldly; |
|||
using Volo.Abp.Security.Claims; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Authentication.OAuth.Claims |
|||
{ |
|||
public class MultipleClaimAction_Tests |
|||
{ |
|||
[Fact] |
|||
public void Should_Set_Single_Value() |
|||
{ |
|||
var jObject = JObject.Parse(@"{
|
|||
""sub"": ""71054539-0e48-af28-5e7a-39e4e42d8ea5"", |
|||
""role"": ""admin"" |
|||
}");
|
|||
|
|||
var claimsIdentity = new ClaimsIdentity(); |
|||
new MultipleClaimAction(AbpClaimTypes.Role, "role").Run(jObject, claimsIdentity, null); |
|||
var claims = claimsIdentity.FindAll(AbpClaimTypes.Role).ToList(); |
|||
claims.Count.ShouldBe(1); |
|||
claims[0].Value.ShouldBe("admin"); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Should_Set_Multiple_Values() |
|||
{ |
|||
var jObject = JObject.Parse(@"{
|
|||
""sub"": ""71054539-0e48-af28-5e7a-39e4e42d8ea5"", |
|||
""role"": [ |
|||
""admin"", |
|||
""moderator"" |
|||
] |
|||
}");
|
|||
|
|||
var claimsIdentity = new ClaimsIdentity(); |
|||
new MultipleClaimAction(AbpClaimTypes.Role, "role").Run(jObject, claimsIdentity, null); |
|||
var claims = claimsIdentity.FindAll(AbpClaimTypes.Role).ToList(); |
|||
claims.Count.ShouldBe(2); |
|||
claims[0].Value.ShouldBe("admin"); |
|||
claims[1].Value.ShouldBe("moderator"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue