目录

Entity Framework Core

基本配置

要将 OpenIddict 配置为使用 Entity Framework Core 作为应用程序、授权、范围和令牌的数据库,您需要:

  • 参考OpenIddict.EntityFrameworkCore

    <PackageReference Include="OpenIddict.EntityFrameworkCore" Version="3.1.1" />
    
  • DbContext创建一个派生自(或IdentityDbContext使用 ASP.NET Core Identity 时)的数据库上下文

    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions options)
            : base(options)
        {
        }
    }
    
  • 配置 OpenIddict 以使用 Entity Framework Core 商店

    services.AddOpenIddict()
        .AddCore(options =>
        {
            options.UseEntityFrameworkCore()
                   .UseDbContext<ApplicationDbContext>();
        });
    
  • 配置 Entity Framework Core 以在模型中注册 OpenIddict 实体

    services.AddDbContext<ApplicationDbContext>(options =>
    {
        // Configure the Entity Framework Core to use Microsoft SQL Server.
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    
        // Register the entity sets needed by OpenIddict.
        options.UseOpenIddict();
    });
    
  • Use migrations or recreate the database to add the OpenIddict entities. For more information, read Migrations Overview.

Advanced configuration

Use a custom primary key type

By default, the Entity Framework Core integration uses string primary keys, which matches the default key type used by ASP.NET Core Identity.

To use a different key type (e.g int, long or Guid):

  • Call the generic ReplaceDefaultEntities<TKey>() method to force OpenIddict to use the default entities with the specified key type:

    services.AddOpenIddict()
        .AddCore(options =>
        {
            // Configure OpenIddict to use the default entities with a custom key type.
            options.UseEntityFrameworkCore()
                   .UseDbContext<ApplicationDbContext>()
                   .ReplaceDefaultEntities<Guid>();
        });
    
  • Configure Entity Framework Core to include the default entities with the chosen key type in the model:

    services.AddDbContext<ApplicationDbContext>(options =>
    {
        // Configure Entity Framework Core to use Microsoft SQL Server.
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    
        // Register the entity sets needed by OpenIddict but use a custom key type.
        options.UseOpenIddict<Guid>();
    });
    

Use custom entities

对于需要存储额外数据以及 OpenIddict 使用的属性的应用程序,可以使用自定义实体。为此,您需要:

  • 创建自定义实体

    public class CustomApplication : OpenIddictEntityFrameworkCoreApplication<long, CustomAuthorization, CustomToken>
    {
        public string CustomProperty { get; set; }
    }
    
    public class CustomAuthorization : OpenIddictEntityFrameworkCoreAuthorization<long, CustomApplication, CustomToken>
    {
        public string CustomProperty { get; set; }
    }
    
    public class CustomScope : OpenIddictEntityFrameworkCoreScope<long>
    {
        public string CustomProperty { get; set; }
    }
    
    public class CustomToken : OpenIddictEntityFrameworkCoreToken<long, CustomApplication, CustomAuthorization>
    {
        public string CustomProperty { get; set; }
    }
    
  • 调用通用ReplaceDefaultEntities<TApplication, TAuthorization, TScope, TToken, TKey>()方法强制 OpenIddict 使用自定义实体

    services.AddOpenIddict()
        .AddCore(options =>
        {
            // Configure OpenIddict to use the custom entities.
            options.UseEntityFrameworkCore()
                   .UseDbContext<ApplicationDbContext>()
                   .ReplaceDefaultEntities<CustomApplication, CustomAuthorization, CustomScope, CustomToken, long>();
        });
    
  • 配置 Entity Framework Core 以在模型中包含自定义实体

    services.AddDbContext<ApplicationDbContext>(options =>
    {
        // Configure Entity Framework Core to use Microsoft SQL Server.
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
    
        // Register the entity sets needed by OpenIddict but use the specified entities instead of the default ones.
        options.UseOpenIddict<CustomApplication, CustomAuthorization, CustomScope, CustomToken, long>();
    });