.net core entity framework – Extract custom field from UserRoles table

I have customized the EF Core identity model to have additional information on the UserRole relationship.
Now, I would like to extract the custom fields added once the user logs in, so I can pass them to the application.

Below are the created classes and the method in which I retrieve the user’s information.

public class ApplicationUser: IdentityUser
{
    public virtual ICollection<ApplicationUserRole> UserRoles { get; set; } = null!;
    public string? UserCode { get; set; } = null!;
    public string? UserExternalCode1 { get; set; } = null!;
    public string? UserExternalCode2 { get; set; } = null!;
    public string? UserExternalCode3 { get; set; } = null!;
    public Guid? EmployeeTypeId { get; set; }
    public EmployeeType? EmployeeType { get; set; } = null!;
}

public class ApplicationUserRole: IdentityUserRole<string>
{
    public Guid? LineId { get; set; }
    public Line? Line { get; set; } = null!;
    public Guid? TerritoryId { get; set; }
    public Territory? Territory { get; set; } = null!;
    public Guid? BusinessUnitId { get; set; }
    public BusinessUnit? BusinessUnit { get; set; } = null!;
    public DateTimeOffset ValidFrom { get; set; }
    public DateTimeOffset? ValidTo { get; set; }
}

ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
 {
     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
     {
     }
     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
         base.OnModelCreating(modelBuilder);
    // User Managment
    modelBuilder.Entity<IdentityUser>().ToTable("Users");
    modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable("RolesClaims");
    modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("UsersClaims");
    modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("UsersLogins");
    modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UsersRoles");
    modelBuilder.Entity<IdentityUserToken<string>>().ToTable("UsersTokens");

    modelBuilder.Entity<ApplicationUser>().Property(p => p.UserCode).HasMaxLength(100);
    modelBuilder.Entity<ApplicationUser>().Property(p => p.UserExternalCode1).HasMaxLength(100);
    modelBuilder.Entity<ApplicationUser>().Property(p => p.UserExternalCode2).HasMaxLength(100);
    modelBuilder.Entity<ApplicationUser>().Property(p => p.UserExternalCode3).HasMaxLength(100);

    modelBuilder.Entity<ApplicationUserRole>().Property(p => p.ValidFrom).HasDefaultValue(DateTimeOffset.Now);
    }

    // User Management
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
    public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; }
}

Extract user and role info

private static async Task<AuthenticationResponseDTO> BuildToken(UsersCredentialsDTO usersCredentialsDTO, IConfiguration configuration, UserManager<IdentityUser> userManager)
 {
     var claims = new List<Claim>
     {
         new Claim("email", usersCredentialsDTO.Email)
     };

     var user = await userManager.FindByEmailAsync(usersCredentialsDTO.Email);
     
     var claimsFromDB = await userManager.GetClaimsAsync(user!);
     var userId = await userManager.GetUserIdAsync(user!);
     var roles = await userManager.GetRolesAsync(user!);
     var userName = await userManager.GetUserNameAsync(user!);

     claims.AddRange(claimsFromDB);

     var key = KeysHandler.GetKey(configuration).First();
     var credential = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

     var expiration = DateTime.UtcNow.AddDays(1);

     var securityToken = new JwtSecurityToken(issuer: null, audience: null, claims: claims, expires: expiration, signingCredentials: credential);

     var token = new JwtSecurityTokenHandler().WriteToken(securityToken);

     return new AuthenticationResponseDTO
     {
         Token = token,
         Id = new Guid(userId),
         Roles = roles,
         Email = userName,
         Claims = claims,
         Expiration = expiration
     };
 }

this instruction var roles = await userManager.GetRolesAsync(user!); is currently returnin only the role Name without the new information added to the entity.

Could someone help me?

I’m quite new to entity framework so probably I’m missing some basics

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật