Теперь я хотел бы извлечь настраиваемые поля, добавляемые после входа пользователя в систему. in, чтобы я мог передать их приложению.
Ниже приведены созданные классы и метод, с помощью которого я получаю информацию о пользователе.
Код: Выделить всё
public class ApplicationUser: IdentityUser
{
public virtual ICollection 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
{
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
{
public ApplicationDbContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// User Management
modelBuilder.Entity().ToTable("Users");
modelBuilder.Entity().ToTable("Roles");
modelBuilder.Entity().ToTable("RolesClaims");
modelBuilder.Entity().ToTable("UsersClaims");
modelBuilder.Entity().ToTable("UsersLogins");
modelBuilder.Entity().ToTable("UsersRoles");
modelBuilder.Entity().ToTable("UsersTokens");
modelBuilder.Entity().Property(p => p.UserCode).HasMaxLength(100);
modelBuilder.Entity().Property(p => p.UserExternalCode1).HasMaxLength(100);
modelBuilder.Entity().Property(p => p.UserExternalCode2).HasMaxLength(100);
modelBuilder.Entity().Property(p => p.UserExternalCode3).HasMaxLength(100);
modelBuilder.Entity().Property(p => p.ValidFrom).HasDefaultValue(DateTimeOffset.Now);
}
// User Management
public DbSet ApplicationUsers { get; set; }
public DbSet ApplicationUserRoles { get; set; }
}
Код: Выделить всё
private static async Task BuildToken(UsersCredentialsDTO usersCredentialsDTO, IConfiguration configuration, UserManager userManager)
{
var claims = new List
{
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
};
}
Код: Выделить всё
var roles = await userManager.GetRolesAsync(user!);
Может ли кто-нибудь мне помочь?
Я новичок в Entity Framework Core, поэтому, возможно, мне не хватает некоторых основ
Подробнее здесь: https://stackoverflow.com/questions/785 ... oles-table