简体   繁体   English

Asp.net Core 2.0的IdentityRole和IdentityUser升级问题

[英]Asp.net core 2.0 upgrade problems with IdentityRole and IdentityUser

I've been working on a web app that uses angular universal and asp.net core with OpenIDDict token auth. 我一直在研究一个Web应用程序,该应用程序使用带有OpenIDDict令牌认证的角度通用和asp.net核心。

I have custom roles and claims / permissions. 我具有自定义角色和声明/权限。 I have taken a lot of code from a previous project that was written for asp.net core 1.1, and as such I had to create extensions to IdentityUser and IdentityRole which I updated as per the microsoft docs, including the navigation properties as shown below, my problem is that if I get the roles from the Db and include Users and Claims eg 我从以前的专为asp.net core 1.1编写的项目中获取了很多代码,因此我必须创建IdentityUser和IdentityRole的扩展名,这些扩展名是根据Microsoft文档更新的,包括如下所示的导航属性,我的问题是,如果我从Db获得角色并包含用户和声明,例如

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

I get the roles, but the user and the claim count is 0, so somewhere something isn't updating, the same happens when getting the user via usermanager, I get a 0 roles and 0 claims! 我得到了角色,但是用户和索取计数为0,所以在某处未更新的情况下,通过usermanager获取用户时,也会发生同样的情况,我得到0个角色和0个索偿! If I use the rolemanager to check the users roles, it works and returns the roles. 如果我使用rolemanager来检查用户角色,它将起作用并返回角色。 I have included my dbContext at the bottom as well. 我也将dbContext包括在底部。 Hopefully it's an easy fix! 希望这是一个简单的解决方法!

ApplicationRole: 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: 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: ApplicationRoleClaim:

  public class ApplicationRoleClaim : IdentityRoleClaim<string>
  {

    public virtual ApplicationRole ApplicationRole { get; set; }

  }

DbContext: 的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);

    }
  }

EDIT: 编辑:

I have noticed that I get the roles count with the user now; 我注意到我现在已经获得了用户的角​​色计数; however, I cant get the user count with the roles. 但是,我无法获得角色的用户数。 I tried to link roles to users but got errors. 我尝试将角色链接到用户,但出现错误。 I did managed to link claims and roles: 我确实设法链接了声明和角色:

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

fixed it! 修复!

Changed my nav properties to match: applicationuser: 将我的导航属性更改为匹配:applicationuser:

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


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

ApplicationRole: ApplicationRole:

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

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

updated my DbContext: 更新了我的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);

Ensured the initial migration looked ok. 确保初始迁移正常。 and it all worked. 而且一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM