如何使用MySQL设置Identity Server 4

时间:2017-04-26 13:08:49

标签: .net identityserver4

我正在尝试将身份服务器4与后端数据库配置为mysql,但无法在Idserver4的官方网站上找到任何指导教程进行配置。 是不是可以使用mysql?

2 个答案:

答案 0 :(得分:0)

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

添加MySql.Data.EntityFrameworkCore nuget包。

作为预防措施,在开始之前,请先将迁移运行到Sql Server中。我们将重新讨论。

通过更改Appsettings.json中的connectionString开始

@EqualsAndHashCode(callSuper = true, of = {})
@Table(name = ProcessEntity.TABLE_NAME)
public class ProcessEntity extends BaseEntity implements ValidityHolder {
public static final String TABLE_NAME = "PROCESS";

@OneToMany(mappedBy = "consent", cascade = CascadeType.ALL)
private Set<ConsentAnswerEntity> consentAnswers;

@OneToMany(mappedBy = "consent", cascade = CascadeType.ALL)
private List<ProcessConsentEntity> processConsents;

@OneToMany(mappedBy = "consent", cascade = CascadeType.ALL)
private Set<ProcessTypeConsentEntity> processTypeConsents;

@Enumerated(EnumType.STRING)
@Column(name = "TYPE_ID")
private Type TpyeId;

请注意,您的数据库密码在此处为纯文本格式,因此,如果更广泛的用户可以看到此文件,请使用用户密码进行提供。

Context定义将需要一些工作,因为MySql会将布尔值作为整数ant持久化,如果未映射,则会导致强制异常。将以下内容添加到ApplicationDbContext的OnModelCreating方法中:

        "DefaultConnection": "server=localhost;userid=root;pwd=rootpassword;persistsecurityinfo=True;port=3306;database=AspIdUsers;sslmode=none;AllowPublicKeyRetrieval=true;"

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

            // 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>()
           ;

更改迁移模块20180109192453_CreateIdentitySchema.Designer和ApplicationDbContextModelSnapshot,以在AspNetUserLogins,AspNetUserRoles,AspNetUserTokens(具有三部分密钥,因此使用长度200)的键字段上设置最大长度。

      services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySQL(connectionString));

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

如果证明无法进行迁移,请从SqlServer导出create语句,并手工制作与MySql等效的语句。

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

那你应该很好。

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

答案 1 :(得分:-1)

services.AddDbContext<yourDbContext>(options => options.UseMySql(yourConnectionName, mySqlOptions => mySqlOptions.CommandTimeout(timeout)));