【问题标题】:UserRole table has duplicate but differently named columns, causes role checking issuesUserRole 表有重复但名称不同的列,导致角色检查问题
【发布时间】:2014-12-19 07:33:56
【问题描述】:

我的项目使用 Microsoft ASP.NET 身份框架,直到最近才使用 2.0 版并正常工作。我当时注意到 UserRole 表有 4 列包含基本相同的数据:

自从升级到 2.1 后,它似乎留下了第二组 [Null] - 就我而言,这是一件好事(我找不到那些额外的列是在哪里定义的。)

modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<ApplicationUser>().ToTable("User");

        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new
        {
            r.RoleId, 
            r.UserId
        }).ToTable("UserRole");;

我去用的时候出现问题:

await _manager.AddToRoleAsync(applicationUser.Id, "Admin");

调用它会填充前两列,而:

var userRoles = await _manager.GetRolesAsync(applicationUser.Id);

似乎要查询第二组列。我知道这一点是因为它们返回 [Null],除非我复制并粘贴前两列的值。

我一直在查看我的代码,寻找任何可能对此有所启发的东西,但我承认此时我完全不知所措。

【问题讨论】:

  • 如果您回到项目并手动执行更新,您的实体模型或表是否需要刷新......这是我能想到的唯一事情。 .
  • 你的意思是powershell中的'update-database'命令吗?如果是这样,是的,我已经做了一些迁移......
  • ApplicationUser_IdIdentityRole_Id 不应在该表中。您需要调整您的模型/dbContext 以取出这些列。
  • 唯一被扩展的表是用户表。从我收集的信息来看,它似乎没有在关键列上找到,而是根据引用的数据库的原始名称创建了附加列。正如我所提到的,在正确的列中创建角色,获取角色似乎是从其他列中获得的。 INSERT 和 SELECT 之间的某处断开连接。

标签: c# asp.net asp.net-web-api asp.net-identity asp.net-identity-2


【解决方案1】:

经过一番明智的搜索,我找到了这个Authorize and GetRoles doesn't work in ASP.NET Identity 帖子。这与对迁移的一些编辑相结合解决了这个问题。

总结:

我加了

 base.OnModelCreating(modelBuilder);

到我的上下文文件 并编辑了模型生成部分

        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");

        //modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        //modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        //modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>(u => u.Roles);
        //modelBuilder.Entity<IdentityUserRole>().HasKey(r => new
        //{
        //    r.RoleId, 
        //    r.UserId
        //});

您的迁移代码尝试删除某些列并重命名其他列,因此我没有删除和重命名,而是删除了要重命名的列并保留了预先存在的列。要删除预先存在的列,您还必须删除外键。我还必须阻止它重新创建主键。

        DropIndex("dbo.UserClaim", new[] { "ApplicationUser_Id" });
        DropIndex("dbo.UserLogin", new[] { "ApplicationUser_Id" });
        DropIndex("dbo.UserRole", new[] { "ApplicationUser_Id" });
        DropIndex("dbo.UserRole", new[] { "IdentityRole_Id" });
        DropForeignKey("FK_dbo_UserClaim_ApplicationUser_Id", "ApplicationUser_Id");
        DropForeignKey("FK_dbo_UserLogin_ApplicationUser_Id", "ApplicationUser_Id");
        DropForeignKey("FK_dbo_UserRole_ApplicationUser_Id", "ApplicationUser_Id");
        DropForeignKey("FK_dbo_UserRole_IdentityRole_Id", "Identity_Id");
        DropColumn("dbo.UserClaim", "ApplicationUser_Id");
        DropColumn("dbo.UserLogin", "ApplicationUser_Id");
        DropColumn("dbo.UserRole", "ApplicationUser_Id");
        DropColumn("dbo.UserRole", "IdentityRole_Id");
        //DropPrimaryKey("dbo.UserLogin");
        //DropPrimaryKey("dbo.UserRole");
        AlterColumn("dbo.User", "Email", c => c.String(maxLength: 256));
        AlterColumn("dbo.User", "UserName", c => c.String(nullable: false, maxLength: 256));
        AlterColumn("dbo.UserClaim", "UserId", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.UserClaim", "UserId", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.UserLogin", "LoginProvider", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.UserLogin", "ProviderKey", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.UserLogin", "UserId", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.UserRole", "UserId", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.UserRole", "RoleId", c => c.String(nullable: false, maxLength: 128));
        AlterColumn("dbo.Role", "Name", c => c.String(nullable: false, maxLength: 256));
        //AddPrimaryKey("dbo.UserLogin", new[] { "LoginProvider", "ProviderKey", "UserId" });
        //AddPrimaryKey("dbo.UserRole", new[] { "UserId", "RoleId" });
        CreateIndex("dbo.User", "UserName", unique: true, name: "UserNameIndex");
        CreateIndex("dbo.UserClaim", "UserId");
        CreateIndex("dbo.UserLogin", "UserId");
        CreateIndex("dbo.UserRole", "UserId");
        CreateIndex("dbo.UserRole", "RoleId");
        CreateIndex("dbo.Role", "Name", unique: true, name: "RoleNameIndex");

这是一次令人沮丧的 Microsoft ASP.NEt Identity 体验,我希望以上内容可以帮助人们多留几缕头发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多