如何使用 MySQL 设置 Identity Server 4

Muk*_*mar 5 .net identityserver4

我正在尝试将 Identity server 4 配置为后端数据库为 mysql,但无法在 Idserver4 的官方网站上找到任何配置指导教程。用mysql可以吗?

Ger*_*Ger 5

重点关注 IdentityServer 示例中的 8_AspNetIdentity 示例。

添加 MySql.Data.EntityFrameworkCore nuget 包。

作为开始之前的预防措施,请运行迁移到 Sql Server。我们将重新讨论这一点。

首先更改 Appsettings.json 中的连接字符串

        "DefaultConnection": "server=localhost;userid=root;pwd=rootpassword;persistsecurityinfo=True;port=3306;database=AspIdUsers;sslmode=none;AllowPublicKeyRetrieval=true;"
Run Code Online (Sandbox Code Playgroud)

请注意,您的数据库密码在此处以纯文本形式显示,因此,如果此文件对更广泛的受众可见,请使用用户机密来提供它。

Context 定义需要一些工作,因为 MySql 将布尔值保留为整数,如果未映射,将导致强制异常。将以下内容添加到 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>()
           ;
Run Code Online (Sandbox Code Playgroud)

告诉 Startup.cs 使用 MySql 而不是 Sql Server:

      services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySQL(connectionString));
Run Code Online (Sandbox Code Playgroud)

更改迁移模块 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");
                });

Run Code Online (Sandbox Code Playgroud)

此列表可能不完整,因此如果您遇到迁移错误,告诉您密钥超出 3072 限制,请记下该表并使用此示例根据需要减少其长度。

如果操作迁移被证明是不可能的,请从 SqlServer 导出创建语句并手工制作它们的 MySql 等效项。

成功创建数据库后,您将需要使用 MySql Workbency 在 AspNetUserClaims 的 Id 列上设置自动增量。

那么你应该可以走了。

如果您选择Combined_AspId_and_EFStorage,事情会变得更加复杂。您无权访问 ConfigurationDbContext 和 PersistedGrantDbContext。它们通过 Indentity Server 4 nuget 包进行管理,因此您需要注入自己的版本才能解决强制问题。


小智 -1

services.AddDbContext<yourDbContext>(options => options.UseMySql(yourConnectionName, mySqlOptions => mySqlOptions.CommandTimeout(timeout)));
Run Code Online (Sandbox Code Playgroud)