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