【问题标题】:IdentityUser Does not Contain Definition for Claim MigrationIdentityUser 不包含声明迁移的定义
【发布时间】:2017-12-10 20:11:31
【问题描述】:

今天,我做出了为 Identity Server 4 升级到 .Net-Core 2.0 的令人沮丧的决定(我并没有太多选择)。

在我的旧应用程序中,我添加了一个内存测试用户作为测试的默认登录名。这使用了 Identity Server 4 教程代码,如下所示:

if (!userManager.Users.Any())
{
    foreach (var inMemoryUser in Users.Get())
    {
        var identityUser = new IdentityUser(inMemoryUser.Username)
        {
            Id = inMemoryUser.SubjectId
        };

        foreach (var claim in inMemoryUser.Claims)
        {
            identityUser.Claims.Add(new IdentityUserClaim<string>
            {
                UserId = identityUser.Id,
                ClaimType = claim.Type,
                ClaimValue = claim.Value,
            });
        }

        userManager.CreateAsync(identityUser, "Password123!").Wait();
    }
}

我收到一个错误:

IdentityUser 不包含声明的定义

我知道这是一个重大更改,正如您在重大更改 announcement 中看到的那样

    /// <summary>
    /// Navigation property for the roles this user belongs to.
    /// </summary>
    public virtual ICollection<TUserRole> Roles { get; } = new List<TUserRole>();

    /// <summary>
    /// Navigation property for the claims this user possesses.
    /// </summary>
    public virtual ICollection<TUserClaim> Claims { get; } = new List<TUserClaim>();

    /// <summary>
    /// Navigation property for this users login accounts.
    /// </summary>
    public virtual ICollection<TUserLogin> Logins { get; } = new List<TUserLogin>();

如果您使用这些导航属性,则需要将它们添加回您的应用程序特定用户类

这是什么意思,我没有特定于应用程序的用户类?

我的身份注册是这样的:

services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>();

我想如何解决这个问题,我想创建一个自定义 IdentityUser 类并添加(声明、角色、登录)属性吗?
我看不出这有什么帮助,因为它们不使用通用界面,它们怎么可能再次被重新填充? 他们是否使用鸭式打字,我的假设是正确的?

【问题讨论】:

  • “他们不使用通用接口,”。您完全不正确,Identity 中的每个类都实现了一个接口。 IdentityUser 实现 IUser&lt;string&gt;。是的,您应该创建自己的课程。默认模板已经为您创建了一个ApplicationUser : IdentityUser
  • @CamiloTerevinto 抱歉,我的问题以前不是很清楚,我知道它们继承自一个通用接口,但我指的是添加到自定义用户类(角色、声明、登录)的 3 个属性,当没有通用接口时这些将如何填充
  • 你有 IdentityUserLoginIdentityUserRoleIdentityUserClaim(类,全部)
  • 是的,即使它们被使用或覆盖,它们仍然与具有 ICollections 的自定义用户类无关。除非它是鸭子类型,否则它将如何填充?
  • @johnny5 如果您迁移 IdentityContext,则在更改使用的用户后,它应该可以再次工作。

标签: c# authentication asp.net-identity identityserver4 asp.net-core-2.0


【解决方案1】:

要让您的代码再次运行,您需要:

自定义您的应用程序用户类(这意味着使用导航属性扩展 IdentityUser); 然后在您的 IdentityContext 中使用您的用户类(作为通用参数),如果尚未完成,最后迁移您的数据库。 然后一切都应该再次运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2011-10-10
    • 2016-06-18
    相关资源
    最近更新 更多