关注 IdentityServer 示例中的 8_AspNetIdentity 示例。
添加 MySql.Data.EntityFrameworkCore nuget 包。
作为开始之前的预防措施,请在 Sql Server 中运行迁移。我们将重新审视这一点。
首先更改 Appsettings.json 中的 connectionString
"DefaultConnection": "server=localhost;userid=root;pwd=rootpassword;persistsecurityinfo=True;port=3306;database=AspIdUsers;sslmode=none;AllowPublicKeyRetrieval=true;"
请注意,您的数据库密码在此处以纯文本形式显示,因此如果此文件对更广泛的受众可见,请使用用户密码来提供它。
上下文定义需要一些工作,因为 MySql 将布尔值作为整数 ant 持久化,如果它们没有被映射,则会导致强制异常。在 ApplicationDbContext 的 OnModelCreating 方法中添加以下内容:
// Conversions required for "No coercion operator is defined between types 'System.Int16' and 'System.Boolean'."
builder
.Entity<ApplicationUser>()
.Property(u => u.EmailConfirmed).HasConversion<Int16>()
;
builder
.Entity<ApplicationUser>()
.Property(u => u.LockoutEnabled).HasConversion<Int16>()
;
builder
.Entity<ApplicationUser>()
.Property(u => u.PhoneNumberConfirmed).HasConversion<Int16>()
;
builder
.Entity<ApplicationUser>()
.Property(u => u.PhoneNumberConfirmed).HasConversion<Int16>()
;
builder
.Entity<ApplicationUser>()
.Property(u => u.TwoFactorEnabled).HasConversion<Int16>()
;
builder
.Entity<ApplicationUser>()
.Property(u => u.PhoneNumberConfirmed).HasConversion<Int16>()
;
告诉 Startup.cs 使用 MySql 而不是 Sql Server:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySQL(connectionString));
更改迁移模块 20180109192453_CreateIdentitySchema.Designer 和 ApplicationDbContextModelSnapshot 以在 AspNetUserLogins、AspNetUserRoles、AspNetUserTokens 的关键字段上设置最大长度(具有三部分键,因此使用长度 200)。
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider")
.HasMaxLength(256);
b.Property<string>("ProviderKey")
.HasMaxLength(256);
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.HasMaxLength(256)
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
此列表可能不完整,因此如果您遇到迁移错误,告诉您您的密钥超出了 3072 限制,请记下该表并使用此示例根据需要减少其长度。
如果证明无法操作迁移,请从 SqlServer 导出 create 语句并手工制作它们的 MySql 等效项。
成功创建数据库后,需要使用 MySql Workbency 设置 AspNetUserClaims 的 Id 列的 AutoIncrement。
那你应该可以走了。
如果您选择 Combined_AspId_and_EFStorage,事情会变得复杂得多。您无法访问 ConfigurationDbContext 和 PersistedGrantDbContext。它们通过 Indentity Server 4 nuget 包进行管理,因此您需要注入自己的版本来解决强制问题。