Asp.net核心2.0升级了IdentityRole和IdentityUser的问题

时间:2017-10-03 12:58:57

标签: entity-framework asp.net-identity asp.net-core-2.0

我一直致力于使用带有OpenIDDict令牌身份验证的角度通用和asp.net核心的网络应用。

我有自定义角色和声明/权限。我从之前为asp.net core 1.1编写的项目中获取了大量代码,因此我必须创建IdentityUser和IdentityRole的扩展,我根据微软文档更新了它,包括如下所示的导航属性,我的问题是,如果我从Db获得角色并包括用户和声明,例如

 _context.Roles
          .Include(r => r.Claims)
          .Include(r => r.Users)
          .OrderBy(r => r.Name);

我获得了角色,但是用户和声明计数是0,所以某些地方没有更新,当通过usermanager获取用户时,同样的情况发生,我得到0个角色和0个声明!如果我使用rolemanager检查用户角色,它将起作用并返回角色。我也在底部包含了我的dbContext。希望这很容易解决!

ApplicationRole:

public class ApplicationRole : IdentityRole
  {

    public ApplicationRole()
    {

    }
    public virtual ICollection<ApplicationRoleClaim> Claims { get; set; } = new List<ApplicationRoleClaim>();
    public virtual ICollection<ApplicationUser> Users { get; set; } = new List<ApplicationUser>();

    public ApplicationRole(string roleName) : base(roleName)
    {

    }


    public ApplicationRole(string roleName, string description) : base(roleName)
    {
      Description = description;
    }






    public string Description { get; set; }
  }

ApplicationUser:

  public class ApplicationUser : IdentityUser
  {
    public virtual string FriendlyName
    {
      get
      {
        string friendlyName = string.IsNullOrWhiteSpace(FullName) ? UserName : FullName;

        if (!string.IsNullOrWhiteSpace(JobTitle))
          friendlyName = JobTitle + " " + friendlyName;

        return friendlyName;
      }
    }


    public string JobTitle { get; set; }
    public string FullName { get; set; }
    public string Configuration { get; set; }
    public bool IsEnabled { get; set; }
    public bool IsLockedOut => this.LockoutEnabled && this.LockoutEnd >= DateTimeOffset.UtcNow;


    public virtual ICollection<IdentityUserRole<string>> Roles { get; } = new List<IdentityUserRole<string>>();


    public virtual ICollection<IdentityUserClaim<string>> Claims { get; } = new List<IdentityUserClaim<string>>();


    public virtual ICollection<IdentityUserLogin<string>> Logins { get; } = new List<IdentityUserLogin<string>>();
    //public ICollection<Order> Orders { get; set; }
  }

ApplicationRoleClaim:

  public class ApplicationRoleClaim : IdentityRoleClaim<string>
  {

    public virtual ApplicationRole ApplicationRole { get; set; }

  }

的DbContext:

  public class MYDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
  {
        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
            Database.EnsureCreated();
        }

        //List of DB Models - Add your DB models here
        public DbSet<Customer> Customers { get; set; }
        public DbSet<Product> Product { get; set; }
        public virtual DbSet<OpenIddictApplication> OpenIddictApplication { get; set; }
        public virtual DbSet<OpenIddictAuthorization> OpenIddictAuthorization { get; set; }
        public virtual DbSet<OpenIddictScope> OpenIddictScope { get; set; }
        public virtual DbSet<OpenIddictToken> OpenIddictToken { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      modelBuilder.Entity<Product>()
          .Property(b => b.EntryTime)
          .HasDefaultValueSql("getdate()");

      base.OnModelCreating(modelBuilder);
      modelBuilder.UseOpenIddict();
      modelBuilder.Entity<ApplicationRoleClaim>()
          .HasOne(pt => pt.ApplicationRole)
          .WithMany(t => t.Claims)
          .HasForeignKey(pt => pt.RoleId);
      modelBuilder.Entity<ApplicationUser>()
          .HasMany(e => e.Claims)
          .WithOne()
          .HasForeignKey(e => e.UserId)
          .IsRequired()
          .OnDelete(DeleteBehavior.Cascade);

      modelBuilder.Entity<ApplicationUser>()
          .HasMany(e => e.Logins)
          .WithOne()
          .HasForeignKey(e => e.UserId)
          .IsRequired()
          .OnDelete(DeleteBehavior.Cascade);

      modelBuilder.Entity<ApplicationUser>()
          .HasMany(e => e.Roles)
          .WithOne()
          .HasForeignKey(e => e.UserId)
          .IsRequired()
          .OnDelete(DeleteBehavior.Cascade);

      modelBuilder.Entity<ApplicationRole>()
    .HasMany(t => t.Claims)
    .WithOne(r => r.ApplicationRole)
    .HasForeignKey(pt => pt.RoleId);

    }
  }

编辑:

我注意到我现在让用户知道角色数量;但是,我无法通过角色获得用户数。我试图将角色链接到用户但却出错了。我确实设法链接了声明和角色:

  modelBuilder.Entity<ApplicationRole>()
    .HasMany(r => r.Claims)
    .WithOne()
    .HasForeignKey(c => c.RoleId).IsRequired()
    .OnDelete(DeleteBehavior.Cascade);

1 个答案:

答案 0 :(得分:1)

修好了!

更改了我的导航属性以匹配: applicationuser:

public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }


public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }

ApplicationRole:

public virtual ICollection<IdentityUserRole<string>> Users { get; set; }

public virtual ICollection<IdentityRoleClaim<string>> Claims { get; set; }

更新了我的DbContext:

  modelBuilder.Entity<ApplicationUser>().HasMany(u => u.Claims).WithOne().HasForeignKey(c => c.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);
  modelBuilder.Entity<ApplicationUser>().HasMany(u => u.Roles).WithOne().HasForeignKey(r => r.UserId).IsRequired().OnDelete(DeleteBehavior.Cascade);

  modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Claims).WithOne().HasForeignKey(c => c.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade);
  modelBuilder.Entity<ApplicationRole>().HasMany(r => r.Users).WithOne().HasForeignKey(r => r.RoleId).IsRequired().OnDelete(DeleteBehavior.Cascade);

确保初始迁移看起来不错。这一切都奏效了。