【问题标题】:Asp.Net Core Identity 2.2 and Identity Server 4, Changing User Id type results in an error in the db contextAsp.Net Core Identity 2.2 和 Identity Server 4,更改用户 ID 类型会导致 db 上下文中出现错误
【发布时间】:2019-11-28 19:57:43
【问题描述】:

得到this code sample成功运行后,我尝试将Identity User Id类型更改为Guid:

public class AppUser : IdentityUser<Guid>
{
    public string Name { get; set; }
}

我在数据库上下文中遇到错误

public class AppIdentityDbContext : IdentityDbContext<AppUser>
{
    public AppIdentityDbContext(DbContextOptions<AppIdentityDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

    }
}

说:

类型“AuthServer.Infrastructure.Data.Identity.AppUser”不能用作类型参数“TUser” 泛型类型或方法“IdentityDbContext”。没有 从 'AuthServer.Infrastructure.Data.Identity.AppUser' 到隐式引用转换 'Microsoft.AspNetCore.Identity.IdentityUser'

猜测该项目中的 Identity Server 没有使用 Asp.Net 角色,我尝试了一种解决方法,方法是创建一个从 IdentityRole 继承的空 AppRole 类,并按如下方式使用它:

public class AppIdentityDbContext : IdentityDbContext<AppUser, AppRole, Guid>

错误停止显示,但在删除迁移文件夹并重新创建新的初始迁移后,我收到以下错误:

访问“程序”类上的 IWebHost 时出错。 在没有应用程序服务提供商的情况下继续。错误: GenericArguments1, 'Microsoft.AspNetCore.Identity.IdentityRole', on 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9[TUser,TRole,TContext,TKey,TUserClaim,TUserRole,TUserLogin,TUserToken,TRoleClaim]' 违反了“TRole”类型的约束。

那么有什么办法呢?

【问题讨论】:

    标签: c# asp.net-core entity-framework-core asp.net-identity identityserver4


    【解决方案1】:

    您可以尝试以下步骤:

    1. 修改AuthServer.Infrastructure中的AppUser

      public class AppUser : IdentityUser<Guid>
      {
          // Add additional profile data for application users by adding properties to this class
          public string Name { get; set; }        
      }
      
    2. 修改AuthServer.Infrastructure中的AppIdentityDbContext

      public class AppIdentityDbContext : IdentityDbContext<AppUser, IdentityRole<Guid>, Guid>
      {
              ...
      }
      
    3. 修改AuthServer中的Startup.cs

      services.AddIdentity<AppUser, IdentityRole<Guid>>()
          .AddEntityFrameworkStores<AppIdentityDbContext>()
          .AddDefaultTokenProviders();
      
    4. 重建AuthServer项目,检查是否有错误:

      修改RegisterResponseViewModel:Id = user.Id.ToString();

      修改AccountController.cs:使用user.Id.ToString()代替user.Id

    5. 删除迁移,dotnet ef migrations add InitialCreate,然后再次更新数据库:dotnet ef database update。在命令中选择带有--context AppIdentityDbContext 的上下文。

    【讨论】:

    • 抱歉,无法将此标记为答案,因为我还无法确保代码正常工作。目前我需要扩展 IdentityRole 实体,因为我有一个强制属性要添加,但是有两个 Roles 实体,一个由 Identity Server 提供,另一个由 AspNet Identity 提供,我想播种一些用户和角色,所以如何扩展 IdentityRole 而不会将应用程序与“鉴别器”列等混淆?
    • 不确定“鉴别器”是什么意思。身份服务器 4 使用 asp.net 身份作为用户/角色管理。那么两个角色实体是什么意思。
    • 我通过添加自定义属性将 IdentityRole 扩展到 AppRole,当我这样做时,我在发布我的用户名和密码时开始看到错误,“鉴别器”是使用时自动添加到表中的列名这样的身份继承,在stackoverflow和其他地方有关于这个“鉴别器”列的问题,但仍然找不到最近的asp.net core 2+相关。
    • 好的,发生上述错误是因为在 startup.cs 我使用的是 services.AddIdentity() 和 AppIdentityDbContext : IdentityDbContext, Guid> 和 AppUserRole :身份用户角色。现在我需要看看为什么我得到两个角色表而不是一个。
    猜你喜欢
    • 2019-06-02
    • 2020-06-21
    • 2021-01-22
    • 2018-06-03
    • 1970-01-01
    • 2021-04-19
    • 2021-11-18
    • 2018-12-16
    • 2019-07-19
    相关资源
    最近更新 更多