|
|
|
@ -13,41 +13,41 @@ public abstract class AbpDynamicClaimsPrincipalContributorBase : IAbpDynamicClai
|
|
|
|
|
|
|
|
|
|
protected virtual async Task MapCommonClaimsAsync(ClaimsIdentity identity, List<AbpClaimCacheItem> dynamicClaims)
|
|
|
|
|
{
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, identity.NameClaimType, "preferred_username");
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, identity.NameClaimType, ClaimTypes.Name);
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, identity.RoleClaimType, "role");
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, identity.RoleClaimType, ClaimTypes.Role);
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, "email", ClaimTypes.Email);
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, "family_name", ClaimTypes.Surname);
|
|
|
|
|
await MapClaimsAsync(identity, dynamicClaims, "given_name", ClaimTypes.GivenName);
|
|
|
|
|
await MapClaimAsync(identity, dynamicClaims, AbpClaimTypes.UserName, "preferred_username", "unique_name", ClaimTypes.Name);
|
|
|
|
|
await MapClaimAsync(identity, dynamicClaims, AbpClaimTypes.Role, "role", "roles", ClaimTypes.Role);
|
|
|
|
|
await MapClaimAsync(identity, dynamicClaims, AbpClaimTypes.Email, "email", ClaimTypes.Email);
|
|
|
|
|
await MapClaimAsync(identity, dynamicClaims, AbpClaimTypes.SurName, "family_name", ClaimTypes.Surname);
|
|
|
|
|
await MapClaimAsync(identity, dynamicClaims, AbpClaimTypes.Name, "given_name", ClaimTypes.GivenName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual Task MapClaimsAsync(ClaimsIdentity identity, List<AbpClaimCacheItem> dynamicClaims, string sourceType, string targetType)
|
|
|
|
|
protected virtual Task MapClaimAsync(ClaimsIdentity identity, List<AbpClaimCacheItem> dynamicClaims, string abpClaimType, params string[] dynamicClaimTypes)
|
|
|
|
|
{
|
|
|
|
|
if (sourceType == targetType)
|
|
|
|
|
var claims = dynamicClaims.Where(c => dynamicClaimTypes.Contains(c.Type)).ToList();
|
|
|
|
|
if (claims.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;;
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (identity.Claims.Any(c => c.Type == sourceType) && dynamicClaims.All(c => c.Type != sourceType))
|
|
|
|
|
foreach (var claimGroup in claims.GroupBy(x => x.Type))
|
|
|
|
|
{
|
|
|
|
|
var claims = dynamicClaims.Where(c => c.Type == targetType).ToList();
|
|
|
|
|
if (!claims.IsNullOrEmpty())
|
|
|
|
|
var claim = claimGroup.First();
|
|
|
|
|
if (claimGroup.Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
identity.RemoveAll(sourceType);
|
|
|
|
|
identity.AddClaims(claims.Select(c => new Claim(sourceType, c.Value)));
|
|
|
|
|
dynamicClaims.RemoveAll(c => c.Type == targetType);
|
|
|
|
|
dynamicClaims.RemoveAll(c => c.Type == claim.Type && identity.Claims.All(x => x.Type != claim.Type));
|
|
|
|
|
identity.RemoveAll(abpClaimType);
|
|
|
|
|
identity.AddClaims(claimGroup.Where(c => c.Value != null).Select(c => new Claim(abpClaimType, c.Value!)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (identity.Claims.Any(c => c.Type == targetType) && dynamicClaims.All(c => c.Type != targetType))
|
|
|
|
|
{
|
|
|
|
|
var claims = dynamicClaims.Where(c => c.Type == sourceType).ToList();
|
|
|
|
|
if (!claims.IsNullOrEmpty())
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
identity.RemoveAll(targetType);
|
|
|
|
|
identity.AddClaims(claims.Select(c => new Claim(targetType, c.Value)));
|
|
|
|
|
dynamicClaims.RemoveAll(c => c.Type == sourceType);
|
|
|
|
|
dynamicClaims.RemoveAll(c => c.Type == claim.Type && identity.Claims.All(x => x.Type != claim.Type));
|
|
|
|
|
if (claim.Value != null)
|
|
|
|
|
{
|
|
|
|
|
identity.AddOrReplace(new Claim(abpClaimType, claim.Value));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
identity.RemoveAll(abpClaimType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -56,16 +56,24 @@ public abstract class AbpDynamicClaimsPrincipalContributorBase : IAbpDynamicClai
|
|
|
|
|
|
|
|
|
|
protected virtual Task AddDynamicClaims(ClaimsIdentity identity, List<AbpClaimCacheItem> dynamicClaims)
|
|
|
|
|
{
|
|
|
|
|
foreach (var claims in dynamicClaims.GroupBy(x => x.Type))
|
|
|
|
|
foreach (var claimGroup in dynamicClaims.GroupBy(x => x.Type))
|
|
|
|
|
{
|
|
|
|
|
if (claims.Count() > 1)
|
|
|
|
|
if (claimGroup.Count() > 1)
|
|
|
|
|
{
|
|
|
|
|
identity.RemoveAll(claims.First().Type);
|
|
|
|
|
identity.AddClaims(claims.Select(c => new Claim(claims.First().Type, c.Value)));
|
|
|
|
|
identity.RemoveAll(claimGroup.First().Type);
|
|
|
|
|
identity.AddClaims(claimGroup.Where(c => c.Value != null).Select(c => new Claim(claimGroup.First().Type, c.Value!)));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
identity.AddOrReplace(new Claim(claims.First().Type, claims.First().Value));
|
|
|
|
|
var claim = claimGroup.First();
|
|
|
|
|
if (claim.Value != null)
|
|
|
|
|
{
|
|
|
|
|
identity.AddOrReplace(new Claim(claimGroup.First().Type, claim.Value));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
identity.RemoveAll(claim.Type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|