ASP .NET Core Identity custom ApiAuthorizationDbContext



我正在使用带有Angular项目的ASP .NET Core 3.0。我看到了这个新ApiAuthorizationDbContext,我想覆盖表名和用户 ID(到 int(,但我无法做到这一点。有没有人知道诡计?

这是上下文覆盖表名的类,但它会创建AspNetUser表并User表。为什么它不像往常一样创建一个?

public class ApplicationDbContext : ApiAuthorizationDbContext<AppUser>
{
public ApplicationDbContext(
DbContextOptions options,
IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AppUser>(entity => { entity.ToTable(name: "User"); });
modelBuilder.Entity<AppRole>(entity => { entity.ToTable(name: "Role"); });
}
}

这是我的用户:

public class AppUser : IdentityUser
{
}

正常情况下,我用AppUser<int>覆盖主键,但由于ApiAuthorizationDbContext而不起作用。

有什么想法吗?

要自定义带有ApiAuthorizationDbContext的用户和角色表,您可以按照以下步骤操作:

  1. 创建具有标识的 Asp.Net 核心角度模板
  2. 添加用户和角色类

    public class AppUser : IdentityUser<int>
    {
    }
    public class AppRole : IdentityRole<int>
    {
    }
    
  3. 添加自定义ApiAuthorizationDbContext

    /// <summary>
    /// Database abstraction for a combined <see cref="DbContext"/> using ASP.NET Identity and Identity Server.
    /// </summary>
    /// <typeparam name="TUser"></typeparam>
    /// <typeparam name="TRole"></typeparam>
    /// <typeparam name="TKey">Key of the IdentityUser entity</typeparam>
    public class KeyApiAuthorizationDbContext<TUser, TRole, TKey> : IdentityDbContext<TUser, TRole, TKey>, IPersistedGrantDbContext
    where TUser : IdentityUser<TKey>
    where TRole : IdentityRole<TKey>
    where TKey : IEquatable<TKey>
    {
    private readonly IOptions<OperationalStoreOptions> _operationalStoreOptions;
    /// <summary>
    /// Initializes a new instance of <see cref="ApiAuthorizationDbContext{TUser, TRole, TKey}"/>.
    /// </summary>
    /// <param name="options">The <see cref="DbContextOptions"/>.</param>
    /// <param name="operationalStoreOptions">The <see cref="IOptions{OperationalStoreOptions}"/>.</param>
    public KeyApiAuthorizationDbContext(
    DbContextOptions options,
    IOptions<OperationalStoreOptions> operationalStoreOptions)
    : base(options)
    {
    _operationalStoreOptions = operationalStoreOptions;
    }
    /// <summary>
    /// Gets or sets the <see cref="DbSet{PersistedGrant}"/>.
    /// </summary>
    public DbSet<PersistedGrant> PersistedGrants { get; set; }
    /// <summary>
    /// Gets or sets the <see cref="DbSet{DeviceFlowCodes}"/>.
    /// </summary>
    public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
    Task<int> IPersistedGrantDbContext.SaveChangesAsync() => base.SaveChangesAsync();
    /// <inheritdoc />
    protected override void OnModelCreating(ModelBuilder builder)
    {
    base.OnModelCreating(builder);
    builder.ConfigurePersistedGrantContext(_operationalStoreOptions.Value);
    }
    }
    /// <summary>
    /// Database abstraction for a combined <see cref="DbContext"/> using ASP.NET Identity and Identity Server.
    /// </summary>
    /// <typeparam name="TUser"></typeparam>
    public class ApiAuthorizationDbContext<TUser> : KeyApiAuthorizationDbContext<TUser, IdentityRole, string>
    where TUser : IdentityUser
    {
    /// <summary>
    /// Initializes a new instance of <see cref="ApiAuthorizationDbContext{TUser}"/>.
    /// </summary>
    /// <param name="options">The <see cref="DbContextOptions"/>.</param>
    /// <param name="operationalStoreOptions">The <see cref="IOptions{OperationalStoreOptions}"/>.</param>
    public ApiAuthorizationDbContext(
    DbContextOptions options,
    IOptions<OperationalStoreOptions> operationalStoreOptions)
    : base(options, operationalStoreOptions)
    {
    }
    }
    
  4. 更改DbContext

    public class ApplicationDbContext : KeyApiAuthorizationDbContext<AppUser, AppRole, int>
    {
    public ApplicationDbContext(
    DbContextOptions options,
    IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
    {
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<AppUser>(entity => { entity.ToTable(name: "User"); });
    modelBuilder.Entity<AppRole>(entity => { entity.ToTable(name: "Role"); });
    }
    }
    
  5. 注册用户和角色

    services.AddDefaultIdentity<AppUser>()
    .AddRoles<AppRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
    services.AddIdentityServer()                
    .AddApiAuthorization<AppUser, ApplicationDbContext>();
    
  6. 删除现有Migrations(如果数据库存在,则可能需要将其删除(。

  7. 运行添加迁移和更新数据库以检查结果。

目前,您需要自定义ApiAuthorizationDbContext,此问题已通过 ApiAuthorizationDbContext 强制 TUser 扩展 IdentityUser 而不是 IdentityUser #9548 并将 IdentityUser 支持添加到 ApiAuthorizationDbContext #13064。获得新东方版本会有一些延迟。

据我所知,ApiAuthorizationDbContext只是实现了IPersistedGrantDbContext。看起来像一个模板类

为了使用自定义密钥,我已经在我的ApplicationDbContext中实现了IPersistedGrantDbContext,请参见下文。

记得调用 modelBuilder.ConfigurePersistedGrantContext(_OperationalStoreOptions.Value(;在 ModelCreate 上,如下所示

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int,
ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim,
ApplicationUserToken>,
IPersistedGrantDbContext
{
private readonly IOptions<OperationalStoreOptions> _OperationalStoreOptions;
public ApplicationDbContext(
DbContextOptions options,
IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options)
{
this._OperationalStoreOptions = operationalStoreOptions;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseLazyLoadingProxies(true);
}
/// <summary>
/// Gets or sets the <see cref="DbSet{PersistedGrant}"/>.
/// </summary>
public DbSet<PersistedGrant> PersistedGrants { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{DeviceFlowCodes}"/>.
/// </summary>
public DbSet<DeviceFlowCodes> DeviceFlowCodes { get; set; }
/// <summary>
/// Gets or sets the <see cref="DbSet{Key}"/>.
/// </summary>
public DbSet<Key> Keys { get; set; }
Task<int> IPersistedGrantDbContext.SaveChangesAsync() => base.SaveChangesAsync();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ConfigurePersistedGrantContext(_OperationalStoreOptions.Value);
}
}

public class ApplicationUser : IdentityUser<int>
{
[Required]
public Guid ApplicationUserRecordId { get; set; }
}
public class ApplicationRole : IdentityRole<int>
{
public ApplicationRole()
{
}
public ApplicationRole(string roleName) : base(roleName)
{
}
}
public class ApplicationRoleClaim : IdentityRoleClaim<int>
{
}
public class ApplicationUserClaim : IdentityUserClaim<int>
{
}
public class ApplicationUserLogin : IdentityUserLogin<int>
{
}
public class ApplicationUserRole : IdentityUserRole<int>
{
}
public class ApplicationUserToken : IdentityUserToken<int>
{
}

相关内容

  • 没有找到相关文章

最新更新