Skip to content

Instantly share code, notes, and snippets.

@molaie
Last active August 24, 2021 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save molaie/dbe781c201239da847672db03d981699 to your computer and use it in GitHub Desktop.
Save molaie/dbe781c201239da847672db03d981699 to your computer and use it in GitHub Desktop.
public class ProfileClaimsService<TUser> : IProfileService
where TUser : class {
private readonly IUserClaimsPrincipalFactory<TUser> _claimsFactory;
private readonly UserManager<TUser> _userManager;
public ProfileClaimsService(UserManager<TUser> userManager, IUserClaimsPrincipalFactory<TUser> claimsFactory) {
_userManager = userManager;
_claimsFactory = claimsFactory;
}
public async Task GetProfileDataAsync(ProfileDataRequestContext context) {
var user = (await _userManager.GetUserAsync(context.Subject));
var principal = await _claimsFactory.CreateAsync(user);
var claims = principal.Claims.ToList();
claims = claims.Where(claim => context.RequestedClaimTypes.Contains(claim.Type)).ToList();
// Add custom claims in token here based on user properties or any other source
var convertedUser = user as UserIdentity;
var claims2 = new List<Claim> {
new Claim(GivenName , convertedUser.FirstName),
new Claim(FamilyName, convertedUser.Lastname),
new Claim("fullname", convertedUser.FirstName + " " + convertedUser.Lastname),
};
claims.AddRange(claims2);
context.IssuedClaims = claims;
}
public async Task IsActiveAsync(IsActiveContext context) {
var user = await _userManager.GetUserAsync(context.Subject);
context.IsActive = (user != null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment