map the roles in the JWToken
This commit is contained in:
56
src/Yavsc/Services/ProfileService.cs
Normal file
56
src/Yavsc/Services/ProfileService.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using System.Security.Claims;
|
||||
using IdentityModel;
|
||||
using IdentityServer8.Models;
|
||||
using IdentityServer8.Services;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Yavsc.Models;
|
||||
|
||||
namespace Yavsc.Services
|
||||
{
|
||||
public class ProfileService : IProfileService
|
||||
{
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
private readonly RoleManager<IdentityRole> _roleManager;
|
||||
public ProfileService(
|
||||
UserManager<ApplicationUser> userManager,
|
||||
RoleManager<IdentityRole> roleManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_roleManager = roleManager;
|
||||
}
|
||||
|
||||
public async Task<List<Claim>> GetClaimsFromUserAsync(ApplicationUser user)
|
||||
{
|
||||
var claims = new List<Claim> {
|
||||
new Claim(JwtClaimTypes.Subject,user.Id.ToString()),
|
||||
new Claim(JwtClaimTypes.PreferredUserName,user.UserName)
|
||||
};
|
||||
|
||||
var role = await _userManager.GetRolesAsync(user);
|
||||
role.ToList().ForEach(f =>
|
||||
{
|
||||
claims.Add(new Claim(JwtClaimTypes.Role, f));
|
||||
});
|
||||
|
||||
|
||||
return claims;
|
||||
}
|
||||
|
||||
|
||||
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
|
||||
{
|
||||
var subjectId = context.Subject.Claims.FirstOrDefault(c => c.Type == "sub").Value;
|
||||
var user = await _userManager.FindByIdAsync(subjectId);
|
||||
context.IssuedClaims = await GetClaimsFromUserAsync(user);
|
||||
}
|
||||
|
||||
|
||||
public async Task IsActiveAsync(IsActiveContext context)
|
||||
{
|
||||
var subjectId = context.Subject.Claims.FirstOrDefault(c => c.Type == "sub").Value;
|
||||
var user = await _userManager.FindByIdAsync(subjectId);
|
||||
context.IsActive = user != null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user