Skip to content

Commit 31f9fe4

Browse files
authored
fix:IdentityClaim (#718)
1 parent 007387c commit 31f9fe4

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.Core/DefaultUserContext.cs

+15-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public DefaultUserContext(
3131
var modelRelation = ModelRelationCache.GetOrAdd(userType, (type) =>
3232
{
3333
var constructor = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public)
34-
.FirstOrDefault(c => c.GetParameters().Length == 0) ??
35-
throw new InvalidOperationException($"[{type.Name}] has a parameterless constructor");
34+
.FirstOrDefault(c => c.GetParameters().Length == 0) ??
35+
throw new InvalidOperationException($"[{type.Name}] has a parameterless constructor");
3636
return new CustomizeModelRelation(
3737
InstanceBuilder.CreateInstanceDelegate(constructor),
3838
InstanceBuilder.GetPropertyAndMethodInfoRelations(type));
@@ -44,7 +44,19 @@ public DefaultUserContext(
4444
if (claimType == null)
4545
continue;
4646

47-
var claimValue = ClaimsPrincipal?.FindClaimValue(claimType);
47+
string? claimValue = null;
48+
if (property.PropertyType != typeof(string) && typeof(System.Collections.IEnumerable).IsAssignableFrom(property.PropertyType))
49+
{
50+
var claimsValues = ClaimsPrincipal?.Claims.Where(claim => claim.Type == claimType)
51+
.Select(claim => claim.Value);
52+
if (claimsValues?.Any() == true)
53+
claimValue = JsonSerializer.Serialize(claimsValues);
54+
}
55+
else
56+
{
57+
claimValue = ClaimsPrincipal?.FindClaimValue(claimType);
58+
}
59+
4860
if (claimValue != null)
4961
{
5062
modelRelation.Setters[property]

0 commit comments

Comments
 (0)