【问题标题】:Custom Identity EntityFrameworkCore自定义身份EntityFrameworkCore
【发布时间】:2016-11-19 05:18:18
【问题描述】:

我正在尝试仅使用 AspNetUsers、AspNetRoles 和 AspNetUserRoles 自定义 EntityFrameworkCore 身份......这可能吗?

我的代码:

ApplicationUser.cs

public class ApplicationUser : IdentityUser<int>
{
}

ApplicationDbContext.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        //base.OnModelCreating(builder);

        builder.Entity<IdentityUser<int>>().ForSqlServerToTable("AspNetUsers")
            //.Ignore(x => x.Claims)
            //.Ignore(x => x.Logins)
            .HasKey(x => x.Id);

        builder.Entity<IdentityRole<int>>().ForSqlServerToTable("AspNetRoles")
            //.Ignore(x => x.Claims)
           .HasKey(x => x.Id);

        builder.Entity<IdentityUserRole<int>>().ForSqlServerToTable("AspNetUserRoles")
            .HasKey(x => new { x.UserId, x.RoleId });

        builder.Ignore<IdentityUserLogin<int>>();
        builder.Ignore<IdentityUserToken<int>>();
        builder.Ignore<IdentityUserClaim<int>>();
        builder.Ignore<IdentityRoleClaim<int>>();
    }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
          ...
            services.AddIdentity<ApplicationUser, IdentityRole<int>>()
                .AddEntityFrameworkStores<ApplicationDbContext, int>()
                .AddDefaultTokenProviders();

            ...
        }

AccountController.cs

public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
    {
        ...
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, isPersistent: false);
                return RedirectToLocal(returnUrl);
            }
            AddErrors(result);
        }
        return View(model);
    }

CreateAsync = 成功,但 SignInAsync 返回 InvalidOperationException:无法为“IdentityUserClaim`1”创建 DbSet,因为该类型未包含在上下文。

我的数据库只包含表 AspNetRoles、AspNetUsers 和 AspNetUserRoles 与默认列(Id 的类型 int)。

谢谢。

【问题讨论】:

  • 为什么要忽略IdentityUserClaim等额外的4行?
  • Ignore IdentityUserLogin != InvalidOperationException:实体类型“Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserLogin”需要定义一个键。
  • 没有额外的行返回 -> IdentityUserLogin 需要定义一个键。 IdentityUserToken 需要定义一个键。 SqlException:对象名称“UserClaims”无效。 SqlException:无效的对象名称“RolerClaims”。
  • 您找到解决方案了吗?

标签: c# asp.net-mvc entity-framework authentication asp.net-identity


【解决方案1】:

我遇到了类似的问题,但我创建了一个继承的 XXXRole。错误是:无法为“XXXRole”创建 DbSet,因为该类型未包含在上下文模型中。

事实证明,我还需要让 AppDbContext 知道正在使用哪个角色,否则它会假定...

所以我将它添加到 IdentityDbContext 的泛型类型中:

public class ApplicationDbContext : IdentityDbContext<XXXUser, XXXRole, string>
{
....
}

在上面的这种情况下,我建议您创建一个继承自“IdentityRole”的新具体类。然后你会做这样的事情:

public class ApplicationDbContext : IdentityDbContext<XXXUser, XXXRole, int>
{
....
}

我相信用户和角色使用相同数据类型的密钥 - 所以您可能还需要创建 XXXUser : IdentityUser

【讨论】:

    【解决方案2】:

    我偶然发现了同样的问题,即使你我的错误是愚蠢的,它也可以帮助将来的人:

    我已经命名了

    public class IdentityDbContext: IdentityDbContext<User> { .... }

    当我引用它时,我没有指定它应该从哪个命名空间IdentityDbContext(*正确的是MyNamespace.IdentityDbContext(doh!))

    【讨论】:

    • 那是因为你不应该给你的类和你继承的类同名。
    猜你喜欢
    • 1970-01-01
    • 2018-09-26
    • 1970-01-01
    • 2017-10-25
    • 2010-12-17
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多