-
-
Save gdyrrahitis/92fdc7486042fb8c7c023372ad701bd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProfileClaimsTransformationService: IClaimsTransformation | |
{ | |
private readonly IUserClaimsService _service; | |
private const string Issuer = "Claims.Transformation.Service.Authority"; | |
public ProfileClaimsTransformationService(IUserClaimsService service) => _service = service; | |
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) | |
{ | |
var identity = principal.Identities.FirstOrDefault(); | |
if (identity == null) | |
{ | |
return null; | |
} | |
var identifier = identity.FindFirst(ClaimTypes.NameIdentifier); | |
if (identifier == null) | |
{ | |
return principal; | |
} | |
var userClaims = (await _service.FindUserClaimsByUserNameAsync(identifier.Value)).ToList(); | |
if (!userClaims.Any()) | |
{ | |
return principal; | |
} | |
var claims = userClaims.Select(c => new Claim(c.Type, c.Value, c.ValueType, GetIssuer(c))).ToList(); | |
claims.AddRange(identity.Claims); | |
var claimsIdentity = new ClaimsIdentity(claims, identity.AuthenticationType); | |
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity); | |
return claimsPrincipal; | |
string GetIssuer(UserClaims claim) | |
{ | |
return string.IsNullOrWhiteSpace(claim.Issuer) ? Issuer : claim.Issuer; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment