【问题标题】:How to setup Identity Server 4 with MySQL如何使用 MySQL 设置 Identity Server 4
【发布时间】:2022-03-04 13:33:06
【问题描述】:

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

【问题讨论】:

  • 你确定吗?该站点的快速入门解释了how to use Entity Framework,它可以针对任何具有 EF 提供程序的数据库。将 EF 配置为使用 MySQL 与将其配置为使用 SQL Server 相同。唯一的问题是是否有用于 MySQL 的 EF Core 提供程序
  • @PanagiotisKanavos 收到错误 Package MySql.Data 6.9.9 is not compatible with netcoreapp1.1
  • MySql.Data 6.9.9 不支持 .NET Core。 7.0.1 可以。首先添加最新的 MySQL.Data 提供程序,然后添加 EF Core 提供程序包。
  • @PanagiotisKanavos 在 startup.cs 中出现错误 UseMySql 不可用 services.AddDbContext(options => options.UseMySql(Configuration.GetConnectionString("DefaultConnection")));

标签: .net identityserver4


【解决方案1】:

关注 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 包进行管理,因此您需要注入自己的版本来解决强制问题。

【讨论】:

    【解决方案2】:
    services.AddDbContext<yourDbContext>(options => options.UseMySql(yourConnectionName, mySqlOptions => mySqlOptions.CommandTimeout(timeout)));
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 2018-01-19
    • 2019-09-20
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多