Last active
August 24, 2021 22:47
-
-
Save molaie/dbe781c201239da847672db03d981699 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 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