public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
注意,我们还指定了int作为用户的主键类型(PK Type)。这是因为我们将自定义主键类型作为IdentityUser的第二个泛型参数。
public class ApplicationRole : IdentityRole
{
public bool IsAdmin { get; set; }
}
同样,我们也指定了int作为角色的主键类型。
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity(entity =>
{
entity.Property(e => e.FirstName)
.HasMaxLength(50);
entity.Property(e => e.LastName)
.HasMaxLength(50);
});
builder.Entity(entity =>
{
entity.Property(e => e.IsAdmin)
.HasDefaultValue(false);
});
}
}
请注意,我们还对自定义属性进行了配置。
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.Configure(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options
上一篇:ASP.NETCore7混合使用共享和专用本地化资源的问题
下一篇:ASP.netcore7InvalidOperationException:Unabletoresolveservicefortype