Aspnet核心身份定制ApiAuthorizationDbContext [英] Aspnet core Identity custom ApiAuthorizationDbContext

查看:249
本文介绍了Aspnet核心身份定制ApiAuthorizationDbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有角度项目的aspnet core 3.0.我看到了这个新的ApiAuthorizationDbContext,我想覆盖表名和用户ID(以int表示),但是我无法做到这一点.有人知道一个把戏吗?

I'm working with aspnet core 3.0 with angular project. I see this new ApiAuthorizationDbContext and I wanted to override the table name and user id (to int) but there is no way I can do it. Does any body know a trick?

  • 这是上下文的类,在其中我告诉他覆盖表名,但是它创建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覆盖主键,但是由于ApiAuthorizationDbContext的原因,它无法正常工作.
    • 有人知道吗?

      推荐答案

      要使用ApiAuthorizationDbContext自定义用户和角色表,可以执行以下步骤:

      For custom the user and role tables with ApiAuthorizationDbContext, you could follow steps below:

      1. 创建具有身份的Asp.Net核心Angular模板
      2. 添加用户和角色类

      1. Create an Asp.Net Core Angular Template with Identity
      2. Add User and Role Class

      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"); });
          }
      }
      

    • 注册用户和角色

    • Register user and role

      services.AddDefaultIdentity<AppUser>()
          .AddRoles<AppRole>()
          .AddEntityFrameworkStores<ApplicationDbContext>();
      
      services.AddIdentityServer()                
          .AddApiAuthorization<AppUser, ApplicationDbContext>();
      

    • 删除现有的Migrations(如果数据库存在,则可能需要将其删除).

    • Delete existing Migrations(if database exist, you may need to delete it).

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

      Run add-migration and update-database to check the result.

      当前,您需要自定义ApiAuthorizationDbContext,已通过 ApiAuthorizationDbContext强制TUser跟踪了此问题扩展了IdentityUser而不是IdentityUser#9548 将IdentityUser支持添加到ApiAuthorizationDbContext#13064 .取得最新版本会有些延迟.

      Currently, you need to custom ApiAuthorizationDbContext, this issue has been tracked through ApiAuthorizationDbContext force TUser to extends IdentityUser instead of IdentityUser #9548 and Add IdentityUser support to ApiAuthorizationDbContext #13064. It will be some delay to get the neweast version.

      这篇关于Aspnet核心身份定制ApiAuthorizationDbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆