使用 Blazor 服务器自定义身份模型
Customizing Identity Model with Blazor Server
我正在评估 Blazor Server (.net core 3.1),在创建测试应用程序的一开始就遇到了一个奇怪的问题。
通常,对于 MVC 和 EF,您会添加/扩展 ApplicationUser class 并插入其他属性。添加迁移时,数据库模型会使用这些属性进行更新。这通常效果很好,并允许为每个用户存储额外的属性,例如出生日期。
在我的 BlazorServer 项目中,我搭建了“视图”并将所有引用从 IdentityUser
更新为我创建的自定义 ApplicationUser
class:
public class ApplicationUser : IdentityUser
{
public string FullName { get; set; }
public string MobileNumber { get; set; }
public string IdentityNumber { get; set; }
}
我已经相应地更新了 Startup.cs
文件:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();
services.AddSingleton<WeatherForecastService>();
...
}
人们会期望 EF 能够确定我在应用程序中使用自定义 IdentityUser
class 并创建相应的迁移,但是,迁移仍然依赖于IdentityUser
模型,参见下面 Up()
方法的迁移输出:
protected override void Up(MigrationBuilder migrationBuilder)
{
...
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false)
},
...
我使用的方法是否正确?我找到了遵循此过程但似乎有效的教程 here。我不是在寻找关于脚手架等的建议。这纯粹是围绕迁移而不是获取额外的属性。我很乐意手动添加它们,但如果我错过了与 Blazor 相关的重要步骤,我想知道并了解如何改变我的方法
你是从IdentityDbContext<ApplicationUser>
那里继承来的吗? :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
我正在评估 Blazor Server (.net core 3.1),在创建测试应用程序的一开始就遇到了一个奇怪的问题。
通常,对于 MVC 和 EF,您会添加/扩展 ApplicationUser class 并插入其他属性。添加迁移时,数据库模型会使用这些属性进行更新。这通常效果很好,并允许为每个用户存储额外的属性,例如出生日期。
在我的 BlazorServer 项目中,我搭建了“视图”并将所有引用从 IdentityUser
更新为我创建的自定义 ApplicationUser
class:
public class ApplicationUser : IdentityUser
{
public string FullName { get; set; }
public string MobileNumber { get; set; }
public string IdentityNumber { get; set; }
}
我已经相应地更新了 Startup.cs
文件:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<ApplicationUser>>();
services.AddSingleton<WeatherForecastService>();
...
}
人们会期望 EF 能够确定我在应用程序中使用自定义 IdentityUser
class 并创建相应的迁移,但是,迁移仍然依赖于IdentityUser
模型,参见下面 Up()
方法的迁移输出:
protected override void Up(MigrationBuilder migrationBuilder)
{
...
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false)
},
...
我使用的方法是否正确?我找到了遵循此过程但似乎有效的教程 here。我不是在寻找关于脚手架等的建议。这纯粹是围绕迁移而不是获取额外的属性。我很乐意手动添加它们,但如果我错过了与 Blazor 相关的重要步骤,我想知道并了解如何改变我的方法
你是从IdentityDbContext<ApplicationUser>
那里继承来的吗? :
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>