using Microsoft.AspNetCore.Identity;
namespace Core.Infrastructure.Identity
public class ApplicationUser : IdentityUser
public ICollection<ApplicationUserRole> UserRoles { get; set; }
}
ApplicationUserRole
using Microsoft.AspNetCore.Identity;
namespace Core.Infrastructure.Identity
public class ApplicationUserRole : IdentityUserRole<string>
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
ApplicationRole
using Microsoft.AspNetCore.Identity;
namespace Core.Infrastructure.Identity
public class ApplicationRole : IdentityRole
public ICollection<ApplicationUserRole> UserRoles { get; set; }
}
ApplicationUserConfigurations
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Core.Persistence.Configurations
public class ApplicationUserConfigurations : IEntityTypeConfiguration<ApplicationUser>
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
builder.Property(u => u.IsActive).HasDefaultValue(true);
builder.Property(u => u.IsAdmin).HasDefaultValue(false);
builder.Property(u => u.IsPasswordChanged).HasDefaultValue(false);
builder.Property(u => u.IsDeleted).HasDefaultValue(false);
}
ApplicationRoleConfigurations
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Core.Persistence.Configurations
public class ApplicationRoleConfigurations : IEntityTypeConfiguration<ApplicationRole>
public void Configure(EntityTypeBuilder<ApplicationRole> builder)
builder.HasIndex(r => r.Name).IsUnique();
builder.Property(r => r.IsActive).HasDefaultValue(true);
}
ApplicationUserRoleConfigurations
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole<string>>
public void Configure(EntityTypeBuilder<ApplicationUserRole<string>> builder)
builder.HasKey(ur => new { ur.UserId, ur.RoleId });
builder.HasOne(ur => ur.Role)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.RoleId)
.IsRequired();
builder.HasOne(ur => ur.User)
.WithMany(r => r.UserRoles)
.HasForeignKey(ur => ur.UserId)
.IsRequired();
}
DBContext
public class ApplicationDbContext
: IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>,
ApplicationUserRole, IdentityUserLogin<string>,
IdentityRoleClaim<string>, IdentityUserToken<string>>
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
protected override void OnModelCreating(ModelBuilder builder)
builder.ApplyConfigurationsFromAssembly(typeof(ApplicationDbContext).Assembly);
base.OnModelCreating(builder);
// Customize ASP.NET Identity models and override defaults
// such as renaming ASP.NET Identity, changing key types etc.
builder.ApplyConfiguration(new ApplicationUserConfigurations());
builder.ApplyConfiguration(new ApplicationUserRoleConfigurations());
builder.ApplyConfiguration(new IdentityRoleClaimConfigurations());
builder.ApplyConfiguration(new IdentityUserClaimConfigurations());
builder.ApplyConfiguration(new IdentityUserLoginConfigurations());
builder.ApplyConfiguration(new IdentityUserClaimConfigurations());
builder.ApplyConfiguration(new ApplicationRoleConfigurations());
}
using Core.Infrastructure.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
public class ApplicationUserRoleConfigurations : IEntityTypeConfiguration<ApplicationUserRole<string>>
{