gpt4 book ai didi

c# - ASP .NET Core Identity 自定义 ApiAuthorizationDbContext

转载 作者:行者123 更新时间:2023-12-03 15:21:55 34 4
gpt4 key购买 nike

我正在使用 ASP .NET Core 3.0 和 Angular 项目。我看到这个新的 ApiAuthorizationDbContext,我想覆盖表名和用户 ID(到 int),但我无法做到。有谁知道诀​​窍吗?
这是用于覆盖表名称的上下文的类,但它创建 AspNetUserUser 表。为什么它不像往常一样创建一个?

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 自定义用户和角色表,您可以按照以下步骤操作:

  • 创建一个标识为
  • 的 Asp.Net Core Angular 模板
  • 添加用户和角色类
    public class AppUser : IdentityUser<int>
    {
    }
    public class AppRole : IdentityRole<int>
    {
    }
  • 添加自定义 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)
    {
    }
    }
  • 改变 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"); });
    }
    }
  • 注册用户和角色
    services.AddDefaultIdentity<AppUser>()
    .AddRoles<AppRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

    services.AddIdentityServer()
    .AddApiAuthorization<AppUser, ApplicationDbContext>();
  • 删除现有的 Migrations(如果数据库存在,可能需要删除)。
  • 运行 add-migration 和 update-database 检查结果。

  • 目前,您需要自定义 ApiAuthorizationDbContext ,此问题已通过 ApiAuthorizationDbContext force TUser to extends IdentityUser instead of IdentityUser #9548Add IdentityUser support to ApiAuthorizationDbContext #13064 进行跟踪。获得neweast版本会有些延迟。

    关于c# - ASP .NET Core Identity 自定义 ApiAuthorizationDbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58208894/

    34 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com